Skip to content

Similar interfaces (multiple properties with the same names) get falsely mapped #35548

Closed
@andreivanea

Description

@andreivanea

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]

Playground Link:
https://www.typescriptlang.org/play/index.html?ssl=1&ssc=1&pln=47&pc=48#code/KYDwDg9gTgLgBASwHY2FAZgQwMbDgWQE8AxBKAZxgEkU0tc4BvAKDjcQBMAuOJAVwC2AIzQBuVuySYBwHpSjIA5uIC+zZqEixEtDDjxEAysGwQkHGqj0MW7Tj37CxEtgDdMAGz6y48peLtTARkUORgFJGVmNQ1waHhkK3oDQgAVAAsyC11kphd7XkERKAD2dy8fP0jStiCQmDCIqLtgKSEPYG44IQgIDswkVXUAemG4dDJKOGxMch8ABSgIAQQ55lMkKcVgGFIKeABeOAAKAEoeReXV4AAeIj3KSzp9AD44A7fbdigdvigkOCXFZzAB0P3IfVcwGO-A8HjgswRSEIpyG6zMWx2xg2HHeJ3OgKWwNuRhMZmySVe70++R+MD+AKB1zBwAhHihML4cIR5CRKLRG0xMAyWTxZwuROudzSmSgFOeuDeHzydjpDMJV1B4Mh0Nh8MRA350XU6M28CgfCQAEFuUdZoQkNh8dSVexBfAOJgYJg8ZgAO6YBDwJmgzweY4AbW2u0mMDOABo4NHseSE0mdiK5WcALqo-KjT3exC8iP3WNPazAROknEV5LVwgp8x1-TZ42mqYWpCGZZ4O3kB1Os4ur61DEer0+u0BoMa4kgsOR6MPOOnRPJsnmHN5mKjXyb3EzOY8K1QKCYQgd+DoHhl-YthhHPU8vnid2+W+Ng8PvuFbkG5E33HOAGgIGUsh-PFnwAwggLNBE8QjdBE3IRMYGzcRRh9VYTjvR4cn0OAAB8wKbeVK1OCNsyAA

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions