Closed
Description
TypeScript Version: 3.7.2
Search Terms: interfaces subset properties
Code
export interface MyFirstInterface {
id: number;
name: string;
}
export interface MySecondInterface {
id: number;
value: string;
comment: string;
}
export interface MyThirdInterface {
id: number;
value: string;
comment: string;
enabled: boolean;
}
// first case: Promise
const getFirst = (): Promise<MyFirstInterface> => {
return Promise.resolve(null as any);
}
const getSecond = (): Promise<MySecondInterface> => {
return Promise.resolve(null as any);
}
const getThird = (): Promise<MyThirdInterface> => {
return Promise.resolve(null as any);
}
const runAll = async () => {
const data = await Promise.all([getFirst(), getSecond(), getThird()]);
//data is [MyFirstInterface, MySecondInterface, MySecondInterface]
}
const runSome = async () => {
const data = await Promise.all([getFirst(), getSecond()]);
}
// second case: Array
const f: MyFirstInterface = null as any;
const s: MySecondInterface = null as any;
const t: MyThirdInterface = null as any;
const a = [f, s, t];
//a is (MyFirstInterface | MySecondInterface)[]
Expected behavior:
data should be [MyFirstInterface, MySecondInterface, MyThirdInterface ]
Actual behavior:
data is [MyFirstInterface, MySecondInterface, MySecondInterface]
The two interfaces, MySecondInterface
and MyThirdInterface
, are almost identical. All the properties from MySecondInterface
ar found on MyThirdInterface
but MyThirdInterface
has at least one additional one. Due to (project) constraints extending MySecondInterface
to build MyThirdInterface
(does not work anyway) -- they represent basically different objects, but the have the same properties (not necessarily the ones in the example).