Skip to content
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

Type guard on conditional parameter type #38863

Closed
jer-sen opened this issue May 31, 2020 · 1 comment
Closed

Type guard on conditional parameter type #38863

jer-sen opened this issue May 31, 2020 · 1 comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@jer-sen
Copy link

jer-sen commented May 31, 2020

TypeScript Version: 3.9.2

Search Terms:

Expected behavior: No error, Typescript should guess that a variable of type T extends '1' ? '1' : never will never be false, so that NonFalse<TypeWithCondition<T>> is exactly the same than true | (T extends '1' ? '1' : never)

Actual behavior: Error on logNonFalse(a);

Related Issues: none

Code

type TypeWithCondition<T> = boolean | (T extends '1' ? '1' : never);

type NonFalse<T> = Exclude<T, false>;

function logNonFalse<T>(b: NonFalse<TypeWithCondition<T>>) {
    console.log(b);
};

function test1<T>(a: TypeWithCondition<T>) {
    if (a === false) throw new Error("Can't be false");
    logNonFalse(a);
    // Error : Argument of type 'true | ([T] extends ["1"] ? "1" : never)' is not assignable to parameter of type 'true'.
    //  Type '[T] extends ["1"] ? "1" : never' is not assignable to type 'true'.
    //    Type '"1"' is not assignable to type 'true'.(2345)
};
Output
"use strict";
function logNonFalse(b) {
    console.log(b);
}
;
function test1(a) {
    if (a === false)
        throw new Error("Can't be false");
    logNonFalse(a);
    // Error : Argument of type 'true | ([T] extends ["1"] ? "1" : never)' is not assignable to parameter of type 'true'.
    //  Type '[T] extends ["1"] ? "1" : never' is not assignable to type 'true'.
    //    Type '"1"' is not assignable to type 'true'.(2345)
}
;
Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "alwaysStrict": true,
    "esModuleInterop": true,
    "declaration": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "moduleResolution": 2,
    "target": "ES2017",
    "jsx": "React",
    "module": "ESNext"
  }
}

Playground Link: Provided

@RyanCavanaugh RyanCavanaugh added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Jun 1, 2020
@RyanCavanaugh
Copy link
Member

We can't analyze narrowing like this without negated types (#29317). Even then this might require higher order reasoning than that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

2 participants