Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Saul-Mirone opened this issue Jan 28, 2019 · 3 comments
Open

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

Saul-Mirone opened this issue Jan 28, 2019 · 3 comments
Labels
Bug A bug in TypeScript Domain: Conditional Types The issue relates to conditional types Domain: Control Flow The issue relates to control flow analysis
Milestone

Comments

@Saul-Mirone
Copy link

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

@DanielRosenwasser
Copy link
Member

Also related: #26970.

@jack-williams
Copy link
Collaborator

This might need #29317 to cancel out Exclude branch of the union using not Function, otherwise you end up with a type/untyped call.

@weswigham
Copy link
Member

This should be fixed by #29317

@weswigham weswigham added Bug A bug in TypeScript In Discussion Not yet reached consensus Domain: Conditional Types The issue relates to conditional types Domain: Control Flow The issue relates to control flow analysis and removed In Discussion Not yet reached consensus labels Jan 29, 2019
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Mar 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: Conditional Types The issue relates to conditional types Domain: Control Flow The issue relates to control flow analysis
Projects
None yet
Development

No branches or pull requests

5 participants