I am trying to define a type which accepts residual properties as follows:
export type Base = {
numberProperty: number;
booleanProperty: boolean;
};
export type Residual = {
[key: string]: string;
};
export type Complete = Base & Residual;
const abc: Complete = {
numberProperty: 1234,
booleanProperty: true,
residualProperty: 'abc',
};
In other words, I want to make sure numberProperty
and booleanProperty
are always of number
and boolean
type respectively, but any other property should be string
. However, when compiling this (3.9.2), I get the following error:
error TS2322: Type '{ numberProperty: number; booleanProperty: true; residualProperty: string; }' is not assignable to type 'Complete'.
Type '{ numberProperty: number; booleanProperty: true; residualProperty: string; }' is not assignable to type 'Residual'.
Property 'numberProperty' is incompatible with index signature.
Type 'number' is not assignable to type 'string'.
13 const abc: Complete = {
~~~
Found 1 error.
I've found similar questions and documentation that addresses similar issues, but I have yet to find a conclusive answer of how to do this.