Skip to content

Instantly share code, notes, and snippets.

@kenmori
Last active December 1, 2019 08:15
Show Gist options
  • Save kenmori/c89855dcdd18901289b089725ae374d7 to your computer and use it in GitHub Desktop.
Save kenmori/c89855dcdd18901289b089725ae374d7 to your computer and use it in GitHub Desktop.
まだまだTypeScript

まだまだTypeScript

Recordを使って型を一括変換

type Obj = {
    name: string
    age: number
}

type ObjString = Record<keyof Obj, string | null>

const a: ObjString = { name: "kenji", age: 88 }

// Type 'number' is not assignable to type 'string | null'.

neverは返らない

  • mapped type, `T[keyof T]
type T = {
    name: string
    age: number
    job: never
}
type Type2 = T[keyof T]
//Type2 is  "string" | "number"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment