Support a built-in type for well-formed strings #60765
Labels
Awaiting More Feedback
This means we'd like to hear from more people who would be helped by this feature
Suggestion
An idea for TypeScript
π Search Terms
"Unicode", "well-formed Unicode", "valid Unicode", "lone surrogates", ""UTF-16", "UTF-8", "isWellFormed()", "toWellFormed()"
β Viability Checklist
β Suggestion
ES2024 now has
String.isWellFormed()
andString.toWellFormed()
, which are supported in TypeScript's ES2024 type definitions.But significant value from these functions is not realized in TypeScript because of the lack of a well-formed string type.
What I'd like to see is a "well-formed string" type (itself a super-type of
String
) for whichisWellFormed()
serves as a type guard andtoWellFormed()
(as well as functions likeTextDecoder.decode()
) return the well-formed string type.Additionally, string literals could be determined to be of the well-formed string type at compile time.
This way TypeScript developers could get type safety for scenarios where strings need to be guaranteed to be well-formed.
π Motivating Example
I'm working on a TypeScript implementation of CEL which requires passing well-formed UTF-8 strings into an evaluation environment. If I want to bridge TypeScript's type safety to CEL's type safety, I'll need a well-formed string type in TypeScript.
π» Use Cases
I can do something like this in my project:
But there are some significant disadvantages here:
The text was updated successfully, but these errors were encountered: