Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why this keyof doesn't infer correctly?

I’ve code like this (TS Playground):

const createComp = <T extends {
  modifiers:  {
    [key: string]: string;
  };
  defaultMods: keyof T["modifiers"];
}
>(com: T) => com;

const style = createComp({
  modifiers: {
    blue: 'blue',
    red: 'red'
  },
  defaultMods: 'red' // correct. defaultMods type is 'blue' | 'red'
});

It infer defaultMods type to 'blue' | 'red' as I expected.

But when I try to extract the extends type to Component type, like this (TS Playground):

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

type Component = {
  modifiers:  {
    [key: string]: string;
  };
  defaultMods: keyof Component["modifiers"];
}

const createComp = <T extends Component>(com: T) => com;

const style = createComp({
  modifiers: {
    blue: 'blue',
    red: 'red'
  },
  defaultMods: '' // defaultMods type is string
});

It doesn’t return defaultMods type as 'blue' | 'red'. Did I miss something?

>Solution :

keyof Component["modifiers"] always resolves to string. You will need an extra type parameter to Component to represent the modifiers, and infer that in your function:

type Component<TModifiers extends Record<string, string>> = {
  modifiers: TModifiers;
  defaultMods: keyof TModifiers;
}


const createComp = <TModifiers extends Record<string, string>>(com: Component<TModifiers>) => com;


const style = createComp({
  modifiers: {
    blue: 'blue',
    red: 'red'
  },
  defaultMods: '' // it should be 'blue' | 'red'
});

Playground Link

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading