microsoft / microsoft/TypeScript

If all values used in a template literal have a literal type, then the template should have a literal type

Abierto
#33,757 2 comentarios 6 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

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

Descripción

Search Terms

template string, string literal, template string literal, cast template string as literal

Suggestion

It seems all template strings are widened to type string; however, when all the variables used inside a template string have literal types, it would be nice if the resulting string also had a literal type.

Use Cases

I maintain a library, and I'd like it if templated strings for CSS properties like box shadow would show up for my dev users in intellisense, instead of just "string".

Here's a screenshot showing the current behavior:

image

It would be very useful if readonly boxShadow and readonly border had literal types instead of a generic string type, to inform my devs what value is passed.

Examples


const elementColors = {
  ...
  globalFocus: '#ff0000',
  ...
} as const

const widths = {
  ...
  default: 4,
  ...
} as const

const shadow = {
  default: {
    boxShadow: boxShadow: `${ elementColors.globalFocus } 0 0 0 ${ width.default }px`,
  },
  ...
} as const

In the above, typeof elementColors.globalFocus === '#ff0000' and typeof width.default === 4, but typeof shadow.default.boxShadow === string. All of the parts used to create the template have literal types, so why doesn't the resulting string have a literal type? TypeScript should be able to infer that only a single possible value is valid for this. typeof shadow.default.boxShadow should equal '#ff0000 0 0 0 4px'.

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
    • could be breaking for people who rely on templates having type string vs a literal type
  • 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

Empieza reproduciendo el ejemplo con interpolaciones cuyos valores son literales y compara el tipo inferido de shadow.default.boxShadow con el literal solicitado. Se considera terminado cuando los literales de plantilla cuyos componentes tienen todos tipos literales se infieren como el literal de cadena exacto correspondiente, mientras el comportamiento existente en tiempo de ejecución permanece sin cambios.

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
Bien especificado
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.