microsoft / microsoft/TypeScript

Literal Comparison for Conditional Types

Abierto
#39,103 0 comentarios 2 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Awaiting More Feedback Suggestion
Lenguaje dominante
Go
Estrellas
111k
Forks
14.3k
Merge medio
2 d 4 h
PR fusionados (30 d)
132

Descripción

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.

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

No se nombran archivos, pruebas ni puntos de entrada. Empieza revisando el área del comprobador de tipos de TypeScript responsable de los tipos condicionales y compara la propuesta con el comportamiento existente de las uniones discriminadas; se consideraría terminado cuando exista una sintaxis compatible para comparar tipos con literales que gestione los ejemplos sin cambiar el JavaScript emitido.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
typescript
Área
compilers
Tipo de issue
Nueva funcionalidad
Dificultad
5/5
Tiempo estimado
Más de una semana
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
25/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.