Home > Software engineering >  How to reference derived types in typescript?
How to reference derived types in typescript?

Time:01-31

Having object A:

interface A {
   boolProp: boolean,
   stringProp: string,
   numberProp: number,
   ...other props/methods...
}

Then I want to have objects, which holds one of the A properties and defines default value for it, other properties are irrelevant to this issue.

interface B {
    prop: keyof A,
    default: typeof keyof A,
   ...other props/methods...
}

I want to get TS error when I try to set default value of different type

const b: B = {
  prop: 'stringProp',
  default: true, // expecting to get TS error as true cannot be assigned to string
};

Thank you in advance for any help

  • Related