I'm currently working with a JSON object that looks like this:
{
"elements": [
{
"type": "abstract"
},
{
"type": "machine"
},
{
"type": "user"
}
]
}
These are the types I am dealing with:
export type StateStructure = {
type: 'user' | 'machine' | 'abstract';
};
However, when I try to import the JSON data and assign it to this type using the following function:
export const constructStatesTreeStructure = (
data: StatesRootData = states,
) => ...
I encounter this error message:
Types of property 'type' are incompatible.
Type 'string' is not assignable to type '"abstract" | "user" | "machine"'.
It makes sense as TypeScript doesn't recognize that the values in the JSON 'type' field are limited to just these 3 options. How can I inform or 'cast' to TypeScript that only these 3 values are possible in this JSON?
I attempted to do something like:
type: string & ('user' | 'machine' | 'abstract')
but unfortunately, it didn't work.