While executing the TypeScript code below:
type dataType = {[key: string]: number} | {[key: string]: {[date: string]: number}} | {[key: string]: string;}
const myData1: dataType = {
"name": "Eduardo",
"city": "Miami",
"state": "FL",
"age": 22,
"progress": {"2018": 67, "2019": 76, "2020": 89}
}
It results in an error stating that the object cannot be assigned to the type dataType
even after specifying different types allowed inside the object explicitly. How can I properly allow multiple different types within an object?
This is the warning received:
Type '{ "name": string; "city": string; "state": string; "age": number; }' is not assignable to type 'dataType'.
Type '{ "name": string; "city": string; "state": string; "age": number; }' is not assignable to type '{ [key: string]: string; }'.
Property '"age"' is incompatible with index signature.
Type 'number' is not assignable to type 'string'.
Your assistance will be much appreciated!