Skip to content

Intersection types involving constrained generic type placeholders do not include members of the placeholder type #28752

Closed
@jumpinjackie

Description

@jumpinjackie

TypeScript Version: 3.2.1

Search Terms: intersection / intersection placeholder

Code

interface IFoo {
    name: string;
}

interface IBar {
    aNumber: number;
    anArray: string[];
}

function test<T extends IFoo>() {
    const obj: T & IBar & { anotherProp: boolean } = {
        name: "Testing", //Errors here (TS 3.2.1). Not in TS 3.1.4
        aNumber: 1,
        anArray: ["a", "b", "c"],
        anotherProp: true
    };
}

function test2<T extends IFoo = IFoo>() {
    const obj: T & IBar & { anotherProp: boolean } = {
        name: "Testing", //Errors here (TS 3.2.1). Not in TS 3.1.4
        aNumber: 1,
        anArray: ["a", "b", "c"],
        anotherProp: true
    };
}

Expected behavior:

name being a property of the generic placeholder T (because it's constrained to something that must be or extend from IFoo) should be a valid property of the above intersection type.

Actual behavior:

TS compiler error on the name property (Object literal may only specify known properties and 'name' does not exist ... forgot the actual TS error code here).

This did not happen in TS 3.1.4 (which I had upgraded from) and possibly older versions too

Playground Link: (https://bit.ly/2rfqphb) Had to use bit.ly to shorten the playground link because playground because the < and > characters mess up markdown link formatting here.

Related Issues:

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions