Seeking a more stringent approach regarding object keys in TypeScript.
type UnionType = '1' | '2' | '3'
type TypeGuardedObject = { [key in UnionType]: number }
const exampleObject: TypeGuardedObject = {
'1': 1,
'2': 2,
'3': 3
}
// This is acceptable since '1' is part of the type UnionType.
const test1 = exampleObject['1']
// This should be flagged as an error because the key is a string not included in UnionType,
// yet it passes TypeScript build successfully.
const wrongKey: string = 'bad string'
const test2 = exampleObject[wrongKey]
Is there a way to prevent the second example from passing the TypeScript build?
Thank you