microsoft / microsoft/TypeScript

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

Offen
#61,990 4 Kommentare 1 Reaktion 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

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.

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

Reproduziere den gemeldeten generischen Fall zur Zuweisbarkeit von Template-Literalen im verknüpften TypeScript Playground und vergleiche ihn mit den funktionierenden Beispielen `x.${Name}` und den nicht-generischen Beispielen. Untersuche die Prüfung der Zuweisbarkeit von Template-Literalen im Compiler und stelle sicher, dass die Assertion `x.y.${Name}` kompiliert, ohne die bestehenden Fälle zu beeinträchtigen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

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