microsoft / microsoft/TypeScript

Cascade type narrowing to child constants

Offen
#53,202 0 Kommentare 8 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

Suggestion

🔍 Search Terms

narrow child variable related

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Constants that were assigned a child value of another variable or constant should have their type narrowed alongside their parent.

📃 Motivating Example

Consider the following code:

type SomeUnion = (
  | {type: "string", values: readonly string[]}
  | {type: "number", values: readonly number[]}
  | {type: "date", values: readonly Date[]}
);

function someFunction(union:SomeUnion, index:number): void {
  const value = union.values[index];
  if (union.type === "string") {
    union.values; // readonly string[]
    value; // string | number | Date
  }
}

In someFunction, TypeScript keeps the type of value as it was originally assigned (string | number | Date).

What if dev expects value to have been narrowed to string?

This already happens with the root keys of an object

type AnotherUnion = (
    | {type: "string", sibling: string, foo: {bar: string}}
    | {type: "number", sibling: number, foo: {bar: number}}
    | {type: "date", sibling: Date, foo: {bar: Date}}
);

function manualDestructure(union: AnotherUnion): void {
  const {type, sibling, foo: {bar}} = union;
  if (type === "string") {
    sibling; // string ✔️ 
    bar; // string | number | Date 😿 
  }
}

function argumentDestructure({type, sibling, foo: {bar}}: AnotherUnion): void {
  if (type === "string") {
    sibling; // string ✔️ 
    bar; // string | number | Date 😿 
  }
}

This is observable in both examples.

💻 Use Cases

Use case: Working with discriminated unions holding nested objects and arrays

To achieve such narrowing, one currently must adjust coding style. ie:

function adjustedScope(union:SomeUnion, index:number): void {
  if (union.type === "string") {
    const value = union.values[index];
    value; // string
  } else if (union.type === "number") {
    const value = union.values[index]; // 😭 
    value; // number
  }
}

// or

function typeAssertion(union:SomeUnion, index:number): void {
  const value = union.values[index];
  if (union.type === "string") {
    (value as string); // string
  } else if (union.type === "number") {
    (value as number); // number 😭 & risky?
  }
}

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 mit den motivierenden TypeScript-Beispielen im Issue, insbesondere mit dem indizierten Kindwert und den Fällen mit verschachtelter Destrukturierung. Ermittle, wie sich die Verengung diskriminierter Vereinigungen auf aus Kindeigenschaften abgeleitete Werte auswirken sollte, und überprüfe anschließend, dass die Beispiele wie gezeigt auf string, number oder Date verengt werden.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

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