I try to declare a recursive type with following interface
interface Map<T> {
[key: string]: Map<T> | T;
}
However, when I try to get property of this object:
const map: Map<number> = {
a: {
b: {
c: 2
}
}
};
console.log(map.a.b.c);
I get an error:
TS2339:Property 'b' does not exist on type 'number | Map<number>'. Property 'b' does not exist on type 'number'.
I understand why it happens, but is there a workaround?
P.S. My tsconfig.json is following:
{
"compilerOptions": {
"declaration": true,
"downlevelIteration": true,
"importHelpers": true,
"lib": [
"dom",
"es2017"
],
"module": "es2015",
"moduleResolution": "node",
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"sourceMap": true,
"strict": true,
"target": "es5",
"typeRoots": [
"node_modules/@types"
]
}
}