Skip to content

Strange condtional type behaviour with boolean generic #26897

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

Closed
MarenkovIgor opened this issue Sep 5, 2018 · 2 comments
Closed

Strange condtional type behaviour with boolean generic #26897

MarenkovIgor opened this issue Sep 5, 2018 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@MarenkovIgor
Copy link

MarenkovIgor commented Sep 5, 2018

TypeScript Version: 3.1.0-dev.20180905

Search Terms: condtional types boolean

Code

type Action<T1 = void, T2 = void> =
    T1 extends void ? () => any :
    T2 extends void ? (arg: T1) => any :
    ((arg1: T1, arg2: T2) => any);

let a: Action;

a();

let b: Action<number>;

b(1);

let c: Action<number, string>;

c(1, "");

let d: Action<number, boolean>;

d(1, true); //compilation fails here

Expected behavior: no compilation errors

Actual behavior: error TS2349: Cannot invoke an expression whose type lacks a call signature. Type '((arg1: number, arg2: false) => any) | ((arg1: number, arg2: true) => any)' has no compatible call signatures.

Playground Link:
https://www.typescriptlang.org/play/#src=type%20Action%3CT1%20%3D%20void%2C%20T2%20%3D%20void%3E%20%3D%0D%0A%20%20%20%20T1%20extends%20void%20%3F%20()%20%3D%3E%20any%20%3A%0D%0A%20%20%20%20T2%20extends%20void%20%3F%20(arg%3A%20T1)%20%3D%3E%20any%20%3A%0D%0A%20%20%20%20((arg1%3A%20T1%2C%20arg2%3A%20T2)%20%3D%3E%20any)%3B%0D%0A%0D%0Alet%20a%3A%20Action%3B%0D%0A%0D%0Aa()%3B%0D%0A%0D%0Alet%20b%3A%20Action%3Cnumber%3E%3B%0D%0A%0D%0Ab(1)%3B%0D%0A%0D%0Alet%20c%3A%20Action%3Cnumber%2C%20string%3E%3B%0D%0A%0D%0Ac(1%2C%20%22%22)%3B%0D%0A%0D%0Alet%20d%3A%20Action%3Cnumber%2C%20boolean%3E%3B%0D%0A%0D%0Ad(1%2C%20true)%3B%0D%0A

"Action" type should work similar to C# one: https://dotnetfiddle.net/HAtRbF

@ajafff
Copy link
Contributor

ajafff commented Sep 5, 2018

Duplicate of #24969

@MarenkovIgor
Copy link
Author

#24969 helps

type Action<T1 = void, T2 = void> =
    [T1] extends [void] ? () => any :
    [T2] extends [void] ? (arg: T1) => any :
    ((arg1: T1, arg2: T2) => any)

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Sep 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants