Skip to content

Commit d0cc078

Browse files
deyaaeldeenCopilot
andcommitted
Add TextEncoder and TextDecoder TypeScript type declarations
Both TextEncoder and TextDecoder are now natively available in Hermes: - TextEncoder since React Native 0.74 (facebook/hermes#948) - TextDecoder since React Native 0.85 (facebook/hermes#1855) This adds global type declarations matching the Hermes implementation, following the same interface + var pattern used by all other Web API types in this file. Closes #56325 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent fbc072e commit d0cc078

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

packages/react-native/src/types/globals.d.ts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,5 +695,98 @@ declare global {
695695
readonly LOADING: 1;
696696
};
697697

698+
// #endregion
699+
// #region TextEncoder / TextDecoder
700+
// Available natively in Hermes.
701+
// TextEncoder: since React Native 0.74
702+
// TextDecoder: since React Native 0.85
703+
704+
/**
705+
* Result of {@link TextEncoder.encodeInto}.
706+
*
707+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/TextEncoder/encodeInto#return_value)
708+
*/
709+
interface TextEncoderEncodeIntoResult {
710+
/** The number of UTF-16 units of code read from the input string. */
711+
read: number;
712+
/** The number of bytes written to the destination `Uint8Array`. */
713+
written: number;
714+
}
715+
716+
/**
717+
* TextEncoder takes a stream of code points as input and emits a stream of
718+
* UTF-8 bytes. Hermes only supports UTF-8 encoding.
719+
*
720+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/TextEncoder)
721+
*/
722+
interface TextEncoder {
723+
/** Always `"utf-8"`. [MDN Reference](https://developer.mozilla.org/docs/Web/API/TextEncoder/encoding) */
724+
readonly encoding: 'utf-8';
725+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/TextEncoder/encode) */
726+
encode(input?: string): Uint8Array;
727+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/TextEncoder/encodeInto) */
728+
encodeInto(
729+
input: string,
730+
destination: Uint8Array,
731+
): TextEncoderEncodeIntoResult;
732+
}
733+
734+
var TextEncoder: {
735+
prototype: TextEncoder;
736+
new (): TextEncoder;
737+
};
738+
739+
/**
740+
* Options for the {@link TextDecoder} constructor.
741+
*
742+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/TextDecoder/TextDecoder#options)
743+
*/
744+
interface TextDecoderOptions {
745+
/** If `true`, a `TypeError` is thrown on invalid byte sequences. Defaults to `false`. */
746+
fatal?: boolean | undefined;
747+
/** If `true`, the byte order mark is included in the output. Defaults to `false`. */
748+
ignoreBOM?: boolean | undefined;
749+
}
750+
751+
/**
752+
* Options for {@link TextDecoder.decode}.
753+
*
754+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/TextDecoder/decode#options)
755+
*/
756+
interface TextDecodeOptions {
757+
/**
758+
* If `true`, indicates that additional data will follow in subsequent calls
759+
* to `decode()`. Defaults to `false`.
760+
*/
761+
stream?: boolean | undefined;
762+
}
763+
764+
/**
765+
* TextDecoder takes a stream of bytes as input and emits a stream of code
766+
* points.
767+
*
768+
* Hermes supports the following encodings:
769+
* UTF-8, UTF-16LE, UTF-16BE, and single-byte encodings (ISO-8859-2 through
770+
* ISO-8859-16, Windows-874, Windows-1250 through Windows-1258, KOI8-R,
771+
* KOI8-U, IBM866, Macintosh, x-mac-cyrillic).
772+
*
773+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/TextDecoder)
774+
*/
775+
interface TextDecoder {
776+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/TextDecoder/encoding) */
777+
readonly encoding: string;
778+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/TextDecoder/fatal) */
779+
readonly fatal: boolean;
780+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/TextDecoder/ignoreBOM) */
781+
readonly ignoreBOM: boolean;
782+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/TextDecoder/decode) */
783+
decode(input?: ArrayBufferView | ArrayBuffer, options?: TextDecodeOptions): string;
784+
}
785+
786+
var TextDecoder: {
787+
prototype: TextDecoder;
788+
new (label?: string, options?: TextDecoderOptions): TextDecoder;
789+
};
790+
698791
// #endregion
699792
}

0 commit comments

Comments
 (0)