6
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

2 Answers 2

12

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Why boolean is better? Could you give an example when it makes difference?
@MateuszKisielMateuszKisiel in short, referring to book "Effective TypeScript" by O'Reilly, Item 10: Avoid Object Wrapper Types (String, Number, Boolean, Symbol, BigInt), — otherwise mixup will happen — for example, string is assignable to String, but String is not assignable to string
3

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 { }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.