Let's create a function
type VarietyType = 'choice1' | 'choice2'
const myFunction = (arg: Partial<Record<VarietyType, number>>) => {
console.log(arg)
}
When calling it with an argument of the incorrect type
myFunction({choice1: 1, ['choice3' as 'choice3']: 2})
An error occurs
Argument of type '{ choice1: number; option3: number; }' is not assignable to parameter of type 'Partial<Record<VarietyType, number>>'. Object literal may only specify known properties, and '['option3' as 'option3']' does not exist in type 'Partial<Record<VarietyType, number>>'
This behavior is expected. However, changing the type of the option3
argument to a union type removes the error, which is surprising.
This doesn't trigger an error:
myFunction({option1: 1, ['option3' as 'option3' | 'option4']: 2})
But why is that?