The problem here is not recursion (which is allowed), but with conflicting signatures, as you pointed out.
, . , , . as-is , ; , - . , , , , .
. TypeScript , , , string number. :
interface CSSProperties {
marginLeft?: string | number,
[key: string]: CSSProperties|string|number,
}
, :
let a: CSSProperties = {
marginLeft: 10,
name: {
marginLeft: 20,
}
};
:
let a: CSSProperties = {
marginLeft: 10,
something: false, // Type 'boolean' is not assignable to type 'string | number | CSSProperties'.
something: new RegExp(/a/g), // Type 'RegExp' is not assignable to type 'CSSProperties'.
name: {
marginLeft: 20,
},
car: ["blue"], // Type 'string[]' is not assignable to type 'CSSProperties'.
};
:
let name1: string | number = a.marginLeft; // OK, return type is string | number
a.marginLeft = false; // Blocked, Type 'false' is not assignable to type 'string | number'.
a["whatever"] = false; // Blocked, Type 'false' is not assignable to type 'string | number | CSSProperties'.
a["marginLeft"] = false; // Blocked, Type 'false' is not assignable to type 'string | number'.
, , , - CSSProperties.
:
a["whatever"] = 100;
:
let name3: CSSProperties = a["name"];
, :
let name3: CSSProperties = a["name"] as CSSProperties;