Typescript interface for objects with some known and some unknown property names
Solution 1:
It's not exactly what you want, but you can use a union type:
interface IfcObject {
fixedKey1: string
fixedKey2: string
[key: string]: string | {
param1: number[];
param2: string;
param3: string;
}
}
It covers your case. But the unknown properties could be of type string
.
Solution 2:
export interface IfcObjectValues {
param1: number[];
param2: string;
param3: string;
}
interface MyInterface {
fixedKey1: string,
fixedKey2: number,
[x: string]: IfcObjectValues,
}
Your code in action, see here.
Solution 3:
As @Paleo explained, you can use union property to define an interface for your corresponding object.
I would say you should define an interface for object values and then you should define your original object.
Sample interface can be:
export interface IfcObjectValues {
param1: number[];
param2: string;
param3: string;
}
export interface IfcMainObject {
[key : string]: string | IfcObjectValues;
}