microsoft / microsoft/TypeScript

union of tuples + spread tricks control flow analysis into thinking a reachable branch is not reachable

Offen
#45,522 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bug Domain: check: Control Flow
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

Bug Report

🔎 Search Terms

tuple spread control flow analysis type narrowing

🕗 Version & Regression Information
  • This exists since at least 4.0.5 and is still present in the current 4.4.0-beta.
  • 3.9.7 correctly fails to compile the snippet, but for what appear to be different reasons.
⏯ Playground Link
let current:
  | []
  | [string]
  | [string, string] = [];

const values = ['foo', 'bar', 'baz'];

for (const v of values) {
  if (current.length === 0) {
    current = ['first'];
  } else if (current.length === 1) {
    current; // -> correctly narrows type to [string]
    current = [...current, 'second'];
    current; // -> corrently infers type [string, string]

    // if, instead, we construct the tuple without spreads, the compiler correctly realizes the case below is reachable
    // current = [current[0], 'second'];
  } else {
    assertNever(current); // this should not compile; because this case is actually reachable
    console.log('here!'); // this line is hit, as expected
  }
}

function assertNever(condition: never) {

}
Output
"use strict";
let current = [];
const values = ['foo', 'bar', 'baz'];
for (const v of values) {
    if (current.length === 0) {
        current = ['first'];
    }
    else if (current.length === 1) {
        current; // -> correctly narrows type to [string]
        current = [...current, 'second'];
        current; // -> corrently infers type [string, string]
        // if, instead, we construct the tuple without spreads, the compiler correctly realizes the case below is reachable
        // current = [current[0], 'second'];
    }
    else {
        assertNever(current); // this should not compile; because this case is actually reachable
        console.log('here!'); // this line is hit, as expected
    }
}
function assertNever(condition) {
}

Compiler Options
{
  "compilerOptions": {
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "alwaysStrict": true,
    "esModuleInterop": true,
    "declaration": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "target": "ES2017",
    "jsx": "react",
    "module": "ESNext",
    "moduleResolution": "node"
  }
}

Playground Link: Provided

🙁 Actual behavior

The compiler erroneously claims the last case where current is length 2 cannot be reached (as represented by assertNever compiling without error) when current is reassigned with tuple spread. Furthermore, the compiler correctly identifies that current is a 2-tuple after the spread statement. The declared types also specify that current can be a 2-tuple, which leads me to believe it's the control flow analyzer getting fooled into narrowing the types too far.

The compiler correctly claims the last case can be reached when current is reassigned with each tuple member explicitly stated, which is equivalent to the spread in this case.

🙂 Expected behavior

The usage of spread/explicitly specifying tuple members should both fail to compile.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, das bereitgestellte Tuple-Spread-Beispiel mit den aufgeführten strikten Compileroptionen im TypeScript Playground zu reproduzieren. Verfolge die Kontrollflussanalyse rund um die Längenprüfungen und die erneute Zuweisung des Spreads. Als abgeschlossen gilt die Aufgabe, wenn sowohl Spread- als auch explizit konstruierte Tuple-Zuweisungen den erreichbaren assertNever-Zweig korrekt ablehnen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
42/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.