How can I trigger a type error in TypeScript 4.4.3 for the incorrect string 'c'
below, which is not one of the keys of the object that is passed as the first parameter to the doSomething
method?
const doSomething = ({ a, b }: { a: number, b: string }): boolean => {
return a === 1 || b === 'secret'
}
type SomethingParameterName = keyof Parameters<typeof doSomething>[0]
const orderedParameterNames = [
'b', 'c', 'a' // why no type error for 'c'?
] as SomethingParameterName[]
Check out this code on TypeScript Playground.
I experimented with const
and even tried using 'c' as SomethingParameterName
directly, but still couldn't generate a type error. Unfortunately, in this scenario, I have no easy way to retrieve the list of keys from any source other than the function itself.