Skip to content

Add TextEncoder and TextDecoder TypeScript type declarations#56326

Open
deyaaeldeen wants to merge 1 commit intofacebook:mainfrom
deyaaeldeen:add-textencoder-textdecoder-types
Open

Add TextEncoder and TextDecoder TypeScript type declarations#56326
deyaaeldeen wants to merge 1 commit intofacebook:mainfrom
deyaaeldeen:add-textencoder-textdecoder-types

Conversation

@deyaaeldeen
Copy link
Copy Markdown

@deyaaeldeen deyaaeldeen commented Apr 4, 2026

Hey! Both TextEncoder and TextDecoder are now natively available in Hermes:

But neither is declared in React Native's TypeScript globals (src/types/globals.d.ts), so TypeScript users have to either cast through any, pull in lib: ["dom"], or add their own declarations to use these APIs without type errors.

This adds type declarations for both APIs to globals.d.ts, following the same interface + var pattern used by all other Web API types in the file. The types match what Hermes actually implements:

TextEncoder:

  • encoding (always "utf-8")
  • encode(input?): Uint8Array
  • encodeInto(input, destination): { read, written }

TextDecoder:

  • Constructor accepts an optional encoding label and { fatal?, ignoreBOM? } options
  • encoding, fatal, ignoreBOM getters
  • decode(input?, { stream? }): string
  • Supported encodings: UTF-8, UTF-16LE/BE, and single-byte encodings (ISO-8859-, Windows-, KOI8-*, IBM866, Macintosh, x-mac-cyrillic)

Test Plan

Added type tests in packages/react-native/types/__typetests__/globals.tsx covering:

  • TextEncoder: constructor, encode(), encode(string), encodeInto(), and encoding literal type
  • TextDecoder: constructor (no args, with label, with options), decode() from Uint8Array/ArrayBuffer/empty, decode with { stream } option, and all three getters (encoding, fatal, ignoreBOM)

Ran tsc --noEmit against the type tests — no new errors introduced.

Changelog:

[General] [Added] - Add TypeScript type declarations for TextEncoder and TextDecoder

Closes #56325

@meta-cla
Copy link
Copy Markdown

meta-cla bot commented Apr 4, 2026

Hi @deyaaeldeen!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@deyaaeldeen deyaaeldeen force-pushed the add-textencoder-textdecoder-types branch from d0cc078 to 35b96b6 Compare April 4, 2026 19:33
@meta-cla meta-cla bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 4, 2026
@facebook-github-tools facebook-github-tools bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Apr 4, 2026
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 facebook#56325

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@deyaaeldeen deyaaeldeen force-pushed the add-textencoder-textdecoder-types branch from 35b96b6 to 4b0dac3 Compare April 4, 2026 19:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add TypeScript types for TextEncoder/TextDecoder to global type declarations

1 participant