Typescript create interface having keys of another interface

Viewed 1452
interface A{
    x: string;
    y: string;
    z: string;
    // maybe later something more
}
interface B{
    [K in keyof A]: Boolean;
}

Why isn't it working? I want to create something like that, but dynamically:

interface B{
    x: Boolean;
    y: Boolean;
    z: Boolean;
    // maybe later something more
}
2 Answers

Don't use Boolean use boolean.

Also mapped types only work with type not interface, there is little difference between the two, so this should work

type Bar = {
  [key in keyof A]: boolean
}

Also, this mapped type could be written using the redefined mapped type Record:

type Bar = Record<keyof A, boolean>

Try following:

interface A{
    x: string;
    y: string;
    z: string;
    // maybe later something more
}


type Bar = {
  [key in keyof A]: boolean
}

interface B extends Bar { }
Related