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

Unable to properly split conditional types #35056

Closed
MOZGIII opened this issue Nov 12, 2019 · 2 comments
Closed

Unable to properly split conditional types #35056

MOZGIII opened this issue Nov 12, 2019 · 2 comments

Comments

@MOZGIII
Copy link

MOZGIII commented Nov 12, 2019

TypeScript 3.7.2
Playground link

Compiler Options:

{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "useDefineForClassFields": false,
    "alwaysStrict": true,
    "allowUnreachableCode": false,
    "allowUnusedLabels": false,
    "downlevelIteration": false,
    "noEmitHelpers": false,
    "noLib": false,
    "noStrictGenericChecks": false,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "esModuleInterop": true,
    "preserveConstEnums": false,
    "removeComments": false,
    "skipLibCheck": false,
    "checkJs": false,
    "allowJs": false,
    "experimentalDecorators": false,
    "emitDecoratorMetadata": false,
    "target": "ES2017",
    "module": "ESNext"
  }
}

Input:

const nullAsUndefined = <T>(value: T): T extends null ? undefined : T =>
  value === null ? undefined : value;


console.log(nullAsUndefined(null));
console.log(nullAsUndefined(undefined));
console.log(nullAsUndefined(42));

Output:

"use strict";
const nullAsUndefined = (value) => value === null ? undefined : value;
console.log(nullAsUndefined(null));
console.log(nullAsUndefined(undefined));
console.log(nullAsUndefined(42));

Expected behavior:

There is a type error that I didn't expect.

On the function body:

Type 'T | undefined' is not assignable to type 'T extends null ? undefined : T'.
  Type 'undefined' is not assignable to type 'T extends null ? undefined : T'.(2322)
@nmain
Copy link

nmain commented Nov 12, 2019

Duplicate of #33912

My search terms were "narrowing generic"

@jcalz
Copy link
Contributor

jcalz commented Nov 12, 2019

Duplicate of #22735, related to #33912

@MOZGIII MOZGIII closed this as completed Nov 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants