Skip to content

typeof 'function' using with Exclude<T, Function> #29614

@Saul-Mirone

Description

@Saul-Mirone

TypeScript Version: 3.2.2

Search Terms: Generic typeof

Code

function f2<T>(v: Exclude<T, Function> | (() => T)): T {
    if (typeof v === 'function') {
        v = v() // error!
    }
    return v
}

Expected behavior:
No error, type v is just (() => T).

Actual behavior:
Line 3 report an error: Type '(() => T) | (Exclude<T, Function> & Function)' has no compatible call signatures.

Playground Link: https://www.typescriptlang.org/play/index.html#src=function%20f2%3CT%3E(v%3A%20Exclude%3CT%2C%20Function%3E%20%7C%20(()%20%3D%3E%20T))%3A%20T%20%7B%0D%0A%20%20%20%20if%20(typeof%20v%20%3D%3D%3D%20'function')%20%7B%0D%0A%20%20%20%20%20%20%20%20v%20%3D%20v()%0D%0A%20%20%20%20%7D%0D%0A%20%20%20%20return%20v%0D%0A%7D

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: Conditional TypesThe issue relates to conditional typesDomain: Control FlowThe issue relates to control flow analysis

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions