In TypeScript 4.4.3, how can I cause the incorrect string 'c' below to show a type error, (because it is not one of the keys of the object that is the first parameter of 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[]
See this code at TypeScript Playground.
I played around with const a bit, and directly tried 'c' as SomethingParameterName but that also gives no type error. In this case, I don't have an easy way to get the list of keys from another source than the function itself.
as, which tells compiler you know what the type is. You should useconst orderedParameterNames: SomethingParameterName[]to let the compiler check the type for you.