I'm facing a specific scenario where I need to create an object with a property that can have one of two distinct types (custom classes).
export class MyType1 {
// properties here
}
export class MyType2 {
// properties here
}
class CustomType = MyType1 | MyType2
class Config {
propOne: boolean;
dynamicParam: { [key: string ]: CustomType }
}
The Config object is utilized in the following manner:
let config: Config = {
propertyOne: true,
dynamicParam: {
key1: {
// properties from type one
},
key2: {
// properties from type two
}
}
}
If I explicitly specify the type when defining the object, like so:
key1: <MyType1> {
}
I receive intellisense for properties of MyType1 class. However, if I omit the type specification, I get intellisense for properties of both MyType1 and MyType2 classes (due to dynamicParam's type being CustomType [union type]).
So my question is, can I enforce required type definition when defining the object? Meaning that if I try to define a property as:
key1: {
}
I would receive an error message stating that a type must be specified?