Skip to content

Check on intersection types determines never type #37042

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
jsteuer opened this issue Feb 26, 2020 · 2 comments
Closed

Check on intersection types determines never type #37042

jsteuer opened this issue Feb 26, 2020 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@jsteuer
Copy link

jsteuer commented Feb 26, 2020

TypeScript Version: up to latest version 3.8-Beta

Search Terms:

  • does not exist on type 'never'
  • control flow never
  • null check never
  • type guard null never

Code

let greeting: null | string = null;

/* A error */    function setIt() { greeting = "Hello!" }; setIt()
/* B error */    setTimeout(() => greeting = "Hello!", 0);
/* C error */    const runCallback = (cb: () => void) => cb(); runCallback(() => greeting = "Hello!");
/* D ok    */ // ((() => { greeting = "Hello!" })())
/* E ok    */ // greeting = Math.random() > 0.5 ? "Hello!" : null;

if (greeting) {
    greeting.toUpperCase(); // Property 'toUpperCase' does not exist on type 'never'.
}
if (greeting != null && greeting !== null) {
    greeting.toUpperCase(); // Property 'toUpperCase' does not exist on type 'never'.
}
if (typeof greeting === "string") {
    greeting.toUpperCase(); // Property 'toUpperCase' does not exist on type 'never'.
}

Expected behavior:
greeting should have type string Inside all if-blocks - not never. This should be true for
all (activated exclusively) test cases (A to E).

Actual behavior:
greeting has type never inside the if-blocks (for test cases A, B and C). It appears that the assignments to greeting are not recognized. For test cases D and E everything is working as expected.

Playground Link: https://www.typescriptlang.org/play/?ts=3.8-Beta&ssl=1&ssc=1&pln=17&pc=2#code/DYUwLgBA5gTi4EsB2UBcEkFdjAgHwgGcwZkoIBeDbYAbgFgAoJgegCoIBBCEGGAexgQ2LCGIgAzTEgDGYBPyRFwASTAAKAJQQA3tDiIUlCACIAEiBz8AhCYgBfWsrBqtrDgCEefQcNHjCcAAVBABbEH5MDS1KAD59eHkjKnNLYBsTABoIAAZNBkZ2CABhbwEhEXEIGUViCBhpYoBDHAAjJpkAa2N1GVb0GIp4gDd+BAATbSHq1q0nBqRmto7O9UH42ESyY1SrW3z3CAARCH5u8UqWUTX13QTDchSLPbt7TS1NQ4BRU-OxS9EmwexgAsk0wAALAB0MCaSHG-FCMXiOShAFYIAB+UzPdK2CDoLA4ApMBASCDqIFJKDaHRMKpUshQsD8ACqAAd2bxmoE5hArhAAAoCLkwMAATwgAHIWRzRTyQFKIAiQIQMPxICAAB4IOqKCASrnSpAgYa8KVQpj2UnkykGakQaxUIm4ABkrvuDqdzpotPp4kZKGZbM53KavPy-NEwv4ool0tloZgCqVKrVSA1PB1eqUhpAxtN5stjGtjDJFLz-HJgceFBSxFIKBMfsYDPtTMT8vDID5ApjcclMpDXcCqf4qvVmuzkH1eYLZpgFqtQA

Related Issues: Maybe to #36777

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Feb 26, 2020
@RyanCavanaugh
Copy link
Member

Duplicate #9998

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

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