I'm trying to make a type with one string being an object, but every other string key equal to a string. Here's all the code that I've tried.
interface SubElement {
someProperty: string;
}
This is the object type that I want in the 'subElement'
property of the main object.
interface MainType1 {
[key: string]: string;
/* Error: Property 'subElement' of type 'SubElement' is not assignable to 'string' index type 'string'.ts(2411) */
subElement: SubElement;
}
This one errors within the interface declaration, and it also errors as a type instead of an interface.
type MainType3 = {
/* Error: Property 'subElement' of type 'SubElement' is not assignable to 'string' index type 'string'.ts(2411) */
subElement: SubElement;
[key: Exclude<string, 'subElement'>]: string;
}
This has the same error as the first one.
/* Type doesn't error */
type MainType2 = {
subElement: SubElement;
} & {
[key: string]: string;
}
/* Error: Type '{ subElement: { someProperty: string; }; }' is not assignable to type 'MainType2'.
Type '{ subElement: { someProperty: string; }; }' is not assignable to type '{ [key: string]: string; }'.
Property 'subElement' is incompatible with index signature.
Type '{ someProperty: string; }' is not assignable to type 'string'.ts(2322) */
let someVar: MainType2 = {
subElement: {
someProperty: ''
}
}
This one errors when I try to initialize anything with that type, since it's expecting 'subElement'
to be a string.
/* Type doesn't error */
type MainType4 = {
subElement: SubElement;
} & {
[key: Exclude<string, 'subElement'>]: string;
}
/* Error: Type '{ subElement: { someProperty: string; }; }' is not assignable to type 'MainType2'.
Type '{ subElement: { someProperty: string; }; }' is not assignable to type '{ [key: string]: string; }'.
Property 'subElement' is incompatible with index signature.
Type '{ someProperty: string; }' is not assignable to type 'string'.ts(2322) */
let otherVar: MainType4 = {
subElement: {
someProperty: ''
}
}
Same error as before, 'subElement'
is expected to be a string.
Is this possible or do I need something like {[key: string]: string | SubElement}
?