microsoft / microsoft/TypeScript

Template literal type derived from string is not a subtype of the TLT derived from any

Offen
#59,925 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Domain: Literal Types Help Wanted Possible Improvement
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

🔎 Search Terms

"template literal type", "any", "string", "mutually assignable types"

🕗 Version & Regression Information
  • This changed between versions 4.1.5 and 4.2.3
  • This changed in commit or PR 411c6d04c6007cd3bb74ef0ce6cc11e8c699a2b5 (reported by every-ts)
⏯ Playground Link

https://www.typescriptlang.org/play/?ts=4.2.3#code/C4TwDgpgBA4g9nAJgRigXigAwCQG8B2ArgLYBGEATgL6ZQQAewE+iAzlngIb4g1QD8UYBULQAXFABmnADasIAbigB6ZUJEQAUKEiwEiAEzoOuUgEsA5mfzA+DJi3Y5c3XrUHDRUCdLmKVap5aOtDwSADMxs6kCDIQ3HaMzGwmrnweGt5SsvJKquqi2uCh+gAsUXhEMjKJDinOae4F4tl+eYEaRbphiACsFbiELBCS1hCItclOXDzpzVm+uQHNml3QAEKcKAOswtYWk46ps01BCzn++WfWUKUAdMh3vQA0rfJQN-cGd+FrUJuGHZ7fAWKAAHygRDIlHBUHMVhssJicDi3FhVRksKGiBGYwmtHsU2ObgE8x8F3a80+Dyer0W0Gp31+mgAxnB8LsoHAANYSBonYwAcmQgqgnGmBBI5GomAUmnyAHluR98LcaS8oGziGAZJxrOxGT9WezOWZWKxRHyZiSMMLReKTLsKPsaHKgA

💻 Code
type Good1 = `${number}` extends `${any}` ? true : false; // true
type Good2 = `${bigint}` extends `${any}` ? true : false; // true
type Good3 = `${boolean}` extends `${any}` ? true : false; // true
type Good4 = `${null}` extends `${any}` ? true : false; // true
type Good5 = `${undefined}` extends `${any}` ? true : false; // true

type Bad1 = `${string}` extends `${any}` ? true : false; // true in 4.1.5, false in 4.2.3
type Bad2 = `${string | number | bigint | boolean | null | undefined}` extends `${any}` ? true : false; // true in 4.1.5, false in 4.2.3

const ok: `${any}` = '1' as `${number}`;
// Ok in 4.1.5, complains in 4.2.3
const issue: `${any}` = '1' as `${string}`;
🙁 Actual behavior

Type 'string' is not assignable to type '${any}'.(2322)

🙂 Expected behavior

Should compile with no errors, since `${any}` and string should be the same type (or at least mutually assignable).

Additional information about the issue

Proposed solution: simplify the type `${any}` to string like it is done with the `${string}` type.

Rationale: for any type X that is either concrete or generic (with a restriction to valid arguments of a template literal type) the type `${string|X}` simplifies to string. Therefore, if TypeScript strives to any consistency, we would expect `${string|any}` (i.e. `${any}`) to also simplify to string.

I have no idea why we currently have `${any}` extends `${number}` true (which could break with my suggestion if no other changes are made), but this bug report is not concerted with this. I'm leaving this up to the maintainers.

Proposed solution 2: Just make string extends `${any}` and `${any}` extends string true.

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

Beginne mit der verknüpften TypeScript Playground-Reproduktion und vergleiche das Verhalten zwischen den Versionen 4.1.5 und 4.2.3. Untersuche anschließend den Commit 411c6d04c6007cd3bb74ef0ce6cc11e8c699a2b5 und die von ihm geänderten Pfade des Type-Checkers für Template-Literal-Typen. Als erledigt gilt die Aufgabe, wenn die Zuweisbarkeitsfälle ${string} und ${any} konsistent mit dem erwarteten Verhalten kompiliert werden.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Muss geklärt werden
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

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