Skip to content

Commit 4b0dac3

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 4b0dac3

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## v0.85.0-rc.7
44

5+
### Added
6+
7+
- **TypeScript**: Add global type declarations for `TextEncoder` and `TextDecoder` ([#56326](https://github.com/facebook/react-native/pull/56326) by [@deyaaeldeen](https://github.com/deyaaeldeen))
8+
59
### Fixed
610

711
#### iOS specific

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
}

packages/react-native/types/__typetests__/globals.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,24 @@ const formData = new FormData();
216216
formData.append('file', { fileName: 'example' });
217217
console.log(formData.getParts());
218218
console.log(formData.getAll('username'));
219+
220+
// TextEncoder
221+
const encoder = new TextEncoder();
222+
const encoded: Uint8Array = encoder.encode('hello');
223+
const encodedEmpty: Uint8Array = encoder.encode();
224+
const encoding: 'utf-8' = encoder.encoding;
225+
const encodeIntoResult: TextEncoderEncodeIntoResult = encoder.encodeInto(
226+
'hello',
227+
new Uint8Array(10),
228+
);
229+
console.log(encodeIntoResult.read, encodeIntoResult.written);
230+
231+
// TextDecoder
232+
const decoder = new TextDecoder();
233+
const decoderUtf8 = new TextDecoder('utf-8');
234+
const decoderWithOpts = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true });
235+
const decoded: string = decoder.decode(new Uint8Array([104, 105]));
236+
const decodedFromBuffer: string = decoder.decode(new ArrayBuffer(2));
237+
const decodedEmpty: string = decoder.decode();
238+
const decodedStream: string = decoder.decode(new Uint8Array([0xe6, 0x97]), { stream: true });
239+
console.log(decoder.encoding, decoder.fatal, decoder.ignoreBOM);

0 commit comments

Comments
 (0)