Skip to content

union types not type guarded #32928

Open
Open
@npenin

Description

@npenin

TypeScript Version: 3.5.2

**Search Terms:extended function type restriction

Code

class C
{
    name: string;
    constructor(private handler: F)
    {
        this.name = handler.name;
    }
}

type F = (...args: any[]) => any;

type F2 = F & { inject?: string[] }

function f(x: C | F2):C
{
    if (typeof x == 'function')
        x = new C(f);

    return x;
}

Expected behavior: x in function f should be of type C at the point of the return statement

Actual behavior: x is considered as C or F2, apparently because of the combined type (it works well if we replace F2 with F)

Playground Link: https://www.typescriptlang.org/play/index.html#code/MYGwhgzhAEDCBQBve1XQHZgLYFMBc0EALgE4CW6A5gNwprAD26xJArsEQyQBQAO5ANzBEc0ABZh0AExA4SBAGIBKOqmRoN0ImLIQAdJlzQAvOMky5B7DloaAvvAfwiAT16iFJ6Nz2+wJSggCSRcAbQBdJRMAPmgQ2mc3DwAmL08AMmhEaAoAKxwOAH4CFgpKCOgnADNWdA4yJmgq7gAPAlhoAB9oBWSlPAR1NDIq71d3BlGWk1MAchq6ogb0WZVNNGnTdBwAdzhuKqUEjRIcIlYSdGgW2jsgA

Related Issues: #27143

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issueRescheduledThis issue was previously scheduled to an earlier milestone

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions