Skip to content

BUG: Generic type inference failure #18871

@pmunin

Description

@pmunin

TypeScript Version: 2.5.3 / nightly (2.5.0-dev.201xxxxx)

Code (gist)

type MyArgs = {
    prop1:any;
    prop2?:any;
}

interface IArgsHandler<TArgs extends MyArgs>
{
    handle(args:TArgs):void;
}

function MyFunc<TMyArgs extends MyArgs>(args:TMyArgs, ...handlers:IArgsHandler<TMyArgs>[])
{
    for(let handler of handlers||[])
    {
        handler.handle(args);
    }
}


function exampleOfIssue()
{
    MyFunc({prop1:123, newProp:"asdasd"},{
        handle(a){
            if(a.prop2) //Compilation error here: error TS2551: Property 'prop2' does not exist on type '{ prop1: number; newProp: string; }'. Did you mean 'prop1'?
            {
                console.log("There is prop2");
            }
        }
    })
}

Expected behavior:
Compilation successfull, because TMyArgs extends MyArgs, which has prop2.

Actual behavior:

Compilation error here: error TS2551: Property 'prop2' does not exist on type '{ prop1: number; newProp: string; }'. Did you mean 'prop1'?

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