Upgrade to Pro — share decks privately, control downloads, hide ads and more …

TypeScript の型システム / Type system of the TypeScript

TypeScript の型システム / Type system of the TypeScript

TypeScript の型の表現力と、言語設計のシンプルさ・巧みさについて話しました。

saiya_moebius

July 31, 2020
Tweet

More Decks by saiya_moebius

Other Decks in Programming

Transcript

  1. string number undefined null { x: number, y: number }

    string[] [ string, number ] type Hoge = { x: number, y: number };
  2. & type HogeProps = { x: number }; type HugaProps

    = { y: number }; // 2 const props: HogeProps & HugaProps; const props: { x: number, y: number }; & structural typing props
  3. | type Hoge = { // string object hoge: string

    | { id: string, name: string }; }; // undefined const a: string | undefined; const b: { name?: string; // name string | undefined }; | undefined name?
  4. // c name, id const c: { name: string }

    | { id: string }; // c // name, id const d: { name?: string; id?: string }; c
  5. | let a: "Hoge" | "Huga"; a = "Piyo"; //

    Compile error literal type
  6. let a: string | undefined; if (typeof(a) !== "undefined") {

    // a: string // (string | undefined) - undefined == string } else { // a: undefined // (string | undefined) - !(undefined) == undefined }
  7. never never const a: string; if (typeof(a) !== "string") {

    // string - string == never a.substring(0, 1); // Compile error }
  8. type MyEnum = "Hoge" | "Huga"; // JSON MyEnum //

    switch(JSON.parse(`"INVALID VALUE"`) as MyEnum) { case "Hoge": return something; case "Huga": return something; default: throw new Error(a); } | "Piyo"
  9. type MyEnum = "Hoge" | "Huga" | "Piyo"; // JSON

    MyEnum // switch(JSON.parse(`"INVALID VALUE"`) as MyEnum) { case "Hoge": return something; case "Huga": return something; default: throw new Error(a); // "Piyo" ... } | "Piyo" case "Piyo" default default "INVALID VALUE"
  10. /** enum */ class InvalidMyEnumError extends Error { construct(value: never)

    { // = never super(`Invalid value: ${value}`); } } type MyEnum = "Hoge" | "Huga" | "Piyo"; switch(JSON.parse(`"INVALID VALUE"`) as MyEnum) { case "Hoge": return something; case "Huga": return something; default: // case value: never // value: never // Type '"Piyo"' is not assignable to type 'never'. throw new InvalidMyEnumError(value); }
  11. const b: { name: string, address: string } | {

    id: string }; if (typeof(b.name) === "string") { // { name: "hoge", id: "hoge" } // { id: string } if // b // { name: string, address: string } // } { id: string }
  12. const b: { name : string, id?: never , address:

    string } | { name?: never , id : string }; if (typeof(b.name) === "string") { // name string undefined // b: { name: string } } name?: never
  13. // 2 const x: Readonly<{ name: string }>; const x:

    { readonly name: string }; readonly Readonly<T> Readonly<T> Readonly<T> T
  14. const a: readonly string[]; a[0] = " "; // Compile

    error const b = a; b[0] = " "; // Compile error // readonly const c: string[] = a; // Compile error
  15. keyof type Readonly<T> = { readonly [P in keyof T]:

    T[P]; }; keyof T T keyof { name: string, age: number } "name" | "age"
  16. in type Readonly<T> = { readonly [P in keyof T]:

    T[P]; }; [ in ]: type Hoge = { [P in "name" | "age"]: Error; }; type Hoge = { name: Error; age: Error; };
  17. T[ ] type Readonly<T> = { readonly [P in keyof

    T]: T[P]; }; T[ ] type T = { name: string; age: number; }; type Hoge = { readonly name: T["name"]; readonly age: T["age"]; }; type Hoge = { readonly name: string; readonly age: number; };
  18. type Readonly<T> = { readonly [P in keyof T]: T[P];

    }; keyof T "name" | "age" [P in key of T]: T[P] T["name"] T["age"] readonly
  19. // T Map key, value DeepReadonly ReadonlyMap type DeepReadonly<T> =

    T extends Map<infer K, infer V> ? ReadonlyMap<DeepReadonly<K>, DeepReadonly<V>> : ... // : DeepReadonly TS infer K V
  20. const hoge = [1, 2]; any any[] number[] [ number,

    number ] [ 1, 2 ] [ number, number... ]
  21. const hoge = [1, 2]; any [1,2] " " undefined

    null any[] [1,2] [] [ " ", null ] number[] [1,2] [] [4,3,2,1,-0] [ number, number ] [1,2] [1024,-3.14] [ 1, 2 ] [1,2]
  22. number[] const hoge /* :number[] */ = [1, 2]; hoge.push(3);

    // Valid hoge[0] = 2.71; // Valid hoge[0] = " "; // INVALID [1, 2]
  23. const myEnumFoo = 0; const myEnumBar = 1; type MyEnum

    = typeof myEnumFoo | typeof myEnumBar; // 0 | 1 // hoge readonly [1, 2] const hoge = [1, 2] as const; // const test0: MyEnum = hoge[0]; // Valid (hoge[0] 1 ) const test1: MyEnum = hoge[1]; // INVALID (hoge[1] 2 ) hoge[0] 1
  24. - -