#Get exact field type from interface using `keyof`

3 messages · Page 1 of 1 (latest)

brave dagger
#

How can I get an interface field's exact type when creating a derived type with computed keys?

fossil cedarBOT
#
griest024#0

Preview:```ts
interface Foo {
a: string
b: number
}

type Opts<K extends keyof Foo = keyof Foo> = Record<
K,
Foo[K]

const opts: Opts = {
a: 5, // should force to string
b: "t", // should force to number
}```

brave dagger
#

nevermind I figured it out. I should be using

type Opts = {
  [P in keyof Foo]: Foo[P]
}

syntax