I am reading from a large JSON file.
TypeScript is smart enough to infer the types of all the properties except one.
A simplified example:
type Animal = 'bear' | 'cat' | 'dog';
const data = {
name: 'Max',
age: 3,
animal: 'dog',
// 100s other properties from JSON file...
};
let theName: string = data.name; // perfect
let theAge: number = data.age; // perfect
let theAnimal: Animal = data.animal; // Error: Type 'string' is not assignable to type 'Animal'
data.animal is used in several places, so I'm trying to avoid using as Animal everywhere.
What is the best way to fix this issue?
Is there any simple way I can tell the code that data.animal is an Animal?