microsoft / microsoft/TypeScript

Type '`x.y.${Name}`' does not satisfy the constraint '`${string}.${Name}`'.(2344)

Abierto
#61,990 4 comentarios 1 reacción 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

Template Literal Types, assignability, generics

### 🕗 Version & Regression Information

- Between versions 4.3.5 and 4.2.3 more cases like this started being supported ( `x.${Name}` became assignable to `${string}.${Name}` for example), however the `x.y.${Name}` case has not bees assignable in any version since template support was added up to and including current nightly ( 5.9.0-dev.20250702 )

### ⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.9.0-dev.20250702#code/PQKhCgAIUhhB7AtgBwJYBsCmkAurHYCGAzsZgE464AWhVAgpKsZCcagOYB2hARlrniQAFJgAeOTFwAmxAJSQAQgDooMACpCArmQA0kdinQBPSNMwAzVFyK5jyTAC41kAAY572APoBjaph8Aa0gAXkhyTABHLVQI+lJOHn5MTQAedX0AUTEHH0lpAD4Ablc1YHBxZHhKOwdwqJi4hO4+LDSvRnFJGRZFfUUC0NxyLUwi8HAPOoB3asDiAGVrHxTPBZ9yVGQcABYvAGZUgDlCAkguqVkDHE2uDkGwiOjYzHj2FuS0qEgf1zFlAAkAG8TgQAL6uXTfX7A4g3awcMGAkGnTAQ8DFCZTbAWQgYYjHVHnCSXFhw273IZPRqvZpJNrwVLQtz-YzI0FoyHM1yw+F3JHAjnozGTTyQLjwLgAcSkFFQPgA6nMWI8Gi83olWilGdzWcoAF5cn4woHkhFIw0YopAA

### 💻 Code

```ts
/**
* Compile time assert that A is assignable to (extends) B.
* To use, simply define a type:
* `type _check = requireAssignableTo;`
*/
export type requireAssignableTo<_A extends B, B> = true;

type worksSinceTypeScript4_3 = requireAssignableTo<
`x.${Name}`,
`${string}.${Name}`
>;

type fails = requireAssignableTo<
// Type '`x.y.${Name}`' does not satisfy the constraint '`${string}.${Name}`'.(2344)
`x.y.${Name}`,
`${string}.${Name}`
>;

type nonGenericWorks = requireAssignableTo<
`x.y.z`,
`${string}.z`
>;
```

### 🙁 Actual behavior

A compile error:

> Type '`x.y.${Name}`' does not satisfy the constraint '`${string}.${Name}`'.(2344)

### 🙂 Expected behavior

No compile error. The `.${Name}` portion of the type matches exactly, so this can be reduced to checking if `"x.y"` is assignable to `string` should be allowed.

### Additional information about the issue

I understand that in general not all cases like that are practical to support: perfect assignability checking of template strings is likely infeasible but I decided to report this case as I think this one suggest an algorithm that would be practical to implement and efficient: running a greedy algorithm to remove common constant substrings from the beginning and end of the string, including generic values, might be a viable approach, and would improve handling of this case.

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

Reproduce el caso genérico de asignabilidad de literales de plantilla reportado en el TypeScript Playground enlazado y compáralo con los ejemplos funcionales `x.${Name}` y los ejemplos no genéricos. Investiga la comprobación de asignabilidad de literales de plantilla del compilador y verifica que la aserción `x.y.${Name}` compile sin introducir regresiones en los casos existentes.

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

Evaluación

Stack tecnológico
typescript
Área
compilers
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.