microsoft / microsoft/TypeScript

Allow concatenated strings to be cast as const type

Offen
#54,161 5 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.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

Suggestion

🔍 Search Terms

Concatenated strings const type

✅ Viability 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, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Allow as const cast to be applied to concatenated string literals like so:

// s0 would be 'ab'
const s0 = ('a' + 'b') as const;

https://www.typescriptlang.org/play?#code/FAehAIGcAZwdwPYFcA2ATcAjApuA5AIaZ7ADGCAdpAC5SwC84AFIXuANT7ECU4Bk4clWoBuYKAiQAjOACWAwsTKUaUGY0Vt+glaKA

📃 Motivating Example

It's convenient to split a long string into multiple concatenated lines to enhance code readability, so as const cast should work for it as well.

💻 Use Cases

Currently only a single string is allowed to be cast to its literal type. So as a workaround you just sacrifice code readability and make multiple strings into one:

'a' + 'b' => 'ab'

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 dem verketteten-Literal-Beispiel und den im Issue verlinkten TypeScript-Entwurfszielen. Ermittle, wie as const derzeit ein einzelnes String-Literal im Vergleich zu einer Verkettung behandelt, und identifiziere die Compiler-Tests und das Typprüfungsverhalten, die den Abschluss definieren würden, während das ausgegebene JavaScript erhalten bleibt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
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.