microsoft / microsoft/TypeScript

Literal Comparison for Conditional Types

Offen
#39,103 0 Kommentare 2 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

compare type to value literal, conditional type literals, type is literal, complex schemas

Suggestion

The ability to compare a type against an example of that type, for use in complex type aliases and interfaces.

Use Cases

When designing a complex schema for an object, the actual fields contained by the object can branch in many different ways.

Of course, it's possible to approach the problem by using nullable types for fields that are not present in all cases. This results in a readable type in the error inspection window, but lots of unnecessarily defensive code that would have been solved by a fully typed schema.

It's also possible to create a discriminated union consisting of every branch--but what if you have a lot of branches? It becomes undesirably repetitive and the error inspection window is a wall of text.

Examples

Take for example, the type of a Pipeline:

type Action = 'Data Selection' | 'Data Cleanup' | 'Train' | 'Store' | 'Deploy'
type Status = 'running' | 'finished' | 'error'
type PipelineStep = {
	name: Action
	status: Status
	tags: Array<string>
        // `never` actually always results in a type check error right now
	message: PipelineStep['status'] is 'Error' ? string : never 
	path: PipelineStep['name'] is 'Data Selection' | 'Store' ? string : never
	trainingID: PipelineStep['name'] is 'Train' ? string : never
	endpoint: PipelineStep['name'] is 'Deploy' ? string : never
} 

Contrasted with the current syntax and discriminated unions:

type PipelineStep = {
	name: Action
	status: Exclude<Status, 'Error'>
	tags: Array<String>
} | {
	name: Action
	status: 'Error'
	tags: Array<string>
	message: string
} | // I give up

The number of branches is multiplicative in nature. In this case, the most complex schema could have a maximum of 6 (Actions) x 3 (Status) possible branches.
After optimization, it becomes:

type StepStatus = {
	status: Exclude<Status, 'Error'>
} | {
	status: 'Error'
	message: string
}

type DataSelectionStep = {
	name: 'Data Selection'
	path: string
}
type TrainingStep = { 
	name: 'Training'
	trainingID: string
}
type StoreStep = {
	name: 'Store'
	path: string
}
type DeployStep = { 
	name: 'Deploy'
	endpoint: string
}

type PipelineStep = (DataSelectionStep | TrainingStep | StoreStep | DeployStep | {
	name: Exclude<Action, 'Training' | 'Data Selection' | 'Store' | 'Deploy'>
}) & StepStatus & {
	tags: Array<String>
}

which isn't really much better...

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

Es werden keine Dateien, Tests oder Einstiegspunkte genannt. Beginne mit der Durchsicht des TypeScript-Typprüferbereichs, der für bedingte Typen zuständig ist, und vergleiche den Vorschlag mit dem bestehenden Verhalten diskriminierter Unions; als erledigt würde gelten, wenn eine unterstützte Syntax zum Vergleich von Typen mit Literalen vorhanden ist, die die Beispiele ohne Änderung des ausgegebenen JavaScript verarbeitet.

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
25/100

Neue Issues direkt in Ihr Postfach

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