Replaces every isolated surrogate with U+FFFD REPLACEMENT CHARACTER, so that the result denotes Unicode text and
survives encoding to UTF-8; a string that is already well-formed is returned unaltered. Sanitising before an
operation that must produce bytes turns a hard failure into a visible substitution the caller controls.
Note
Stands in for String.prototype.toWellFormed, standardised in ECMAScript 2024 and declared by TypeScript at the
ES2024 lib level: this package compiles at ES2022, where the native method runs on every supported engine
but remains invisible to the type checker. Will give way to the native method once the compilation target moves
to ES2024.
Parameters
string: string
The string to convert
Returns string
A copy of string with every isolated surrogate replaced by U+FFFD, leaving paired surrogates and all
other code points untouched
See
isWellFormed for the matching test, reporting whether a conversion would alter the string
Converts a string to well-formed UTF-16 text.
Replaces every isolated surrogate with
U+FFFDREPLACEMENT CHARACTER, so that the result denotes Unicode text and survives encoding to UTF-8; a string that is already well-formed is returned unaltered. Sanitising before an operation that must produce bytes turns a hard failure into a visible substitution the caller controls.Stands in for
String.prototype.toWellFormed, standardised in ECMAScript 2024 and declared by TypeScript at theES2024lib level: this package compiles atES2022, where the native method runs on every supported engine but remains invisible to the type checker. Will give way to the native method once the compilation target moves toES2024.