microsoft / microsoft/TypeScript

Probabilistic Types

Abierto
#34,714 5 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

In Discussion Suggestion
Lenguaje dominante
Go
Estrellas
111k
Forks
14.3k
Merge medio
1 d 19 h
PR fusionados (30 d)
117

Descripción

Search Terms

Probabilistic, union, supertypes

Suggestion

If a union type contains a member which is a supertype of one or more other members, the supertype subsumes those other members, but the information isn't lost. Tooling can then use that information as a hint about the expected distribution of possible values within that type.

type MetaSyntacticVar = 'foo' | 'bar' | 'baz' | string;

For the purposes of type safety, the above type is simply string. However, tools may consider 'foo' | 'bar' | 'baz' to be the most likely values for MetaSyntacticVar. An IDE might use them for autocomplete. A linter (i.e. not tsc) might flag other values as a warning. Etc.

Use Cases

A backend service offers a range of options, and I want to statically model them as a union type. Sometimes, the service adds a new option. I must either generalize the type to a string, do a type cast and tolerate some type unsafety, or release code updates in sync with all backend updates.

Examples

type MetaSyntacticVar = 'foo' | 'bar' | 'baz' | string;

const name: MetaSyntacticVar = 'qux';
================================^ IDE suggests foo, bar, or baz as I type this
================================^^^ eslint flags this as a warning (if I opt in)

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

Comienza revisando el union-type checking de TypeScript y el comportamiento de las herramientas descrito para autocomplete y linting. Determina cómo los literal members podrían seguir disponibles como sugerencias mientras el tipo contenedor conserva su comportamiento actual de type-safety. Se considera terminado cuando la propuesta tiene un diseño acordado y cubre el type checker, el IDE tooling y el comportamiento opcional del linter.

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

Evaluación

Stack tecnológico
typescript
Área
compilers, developer-experience
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.