Skip to content

Check on intersection types determines never type #37042

Closed
@jsteuer

Description

@jsteuer

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions