Search code examples
typescriptobjecttypesinterfacekey

Typescript - Any of all interface keys as a type


code

What should be the type of name so that the function getValue only allows for one of Data key types as it argument.


Solution

  • Have you tried keyof Data for the name argument? You can do it like

    const getValue = (name: keyof Data) => data[name];