I have the following TypeScript interface:
interface IX {
a: string;
b: any;
c: AnotherType;
}
I declare a variable of this type and initialize all the properties with default values:
let x: IX = {
a: 'abc',
b: null,
c: null
};
Later, I assign actual values to these properties in an init function:
x.a = 'xyz';
x.b = 123;
x.c = new AnotherType();
I don’t like having to specify default null values for each property when declaring the object, especially since they will be set to real values later. Is there a way to have TypeScript default unspecified properties to null when using an interface, so I can initialize the object like this:
let x: IX = {
a: 'abc'
};
without receiving a compiler error? Currently, I’m getting the error: TS2322: Type ‘{}’ is not assignable to type ‘IX’. Property ‘b’ is missing in type ‘{}’.