microsoft / microsoft/TypeScript

Suggestion: Reference expression type during cast

Offen
#37,729 5 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

Search Terms

cast reference expression type

Suggestion

I would like to be able to reference original type of the expression during cast.
For example:

const o = {
    x: { a: 1 } as typeof & { b?: number }
}

// type of o.x is now { a: number; b?: number }

I am not attached to typeof syntax here, can be anything that makes sense.

Use Cases

  1. Defining optional properties without actually initializing them.
    Current approach would require me to do something like:
const o = {
    x: { a: 1 } as { a: number; b?: number }
}

which is unweildy and repeats all the properties.
It is possible to do through a function, but it has an overhead and is complicated due to partial generics:

function extend<T>(T data) {
    return <TExtra>() => data as T & TExtra;
}

const o = {
    x: extend({ a: 1 })<{ b?: number }>()
}
  1. Adding indexers to object literals:
function map1(key: string) {
    return { a: 1, b: 2 }[key] || 3; // does not work due to https://github.com/microsoft/TypeScript/issues/14951
}

function map2(key: string) {
    return ({ a: 1, b: 2 } as { [key: string]: typeof[keyof typeof]|undefined })[key] || 3;
}

Examples

See use cases.

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, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

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

In der Issue wird keine Quelldatei, kein Test und kein Einstiegspunkt genannt. Beginne damit, die vorgeschlagene Cast-Syntax und beide Anwendungsfälle zu prüfen, und bestimme anschließend das erforderliche Design für Typsystem und Syntax; abgeschlossen wäre dies mit einem akzeptierten Ansatz und dem entsprechenden Implementierungsumfang.

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
Muss geklärt werden
Anfängerfreundlichkeit
30/100

Neue Issues direkt in Ihr Postfach

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