Skip to content

Commit 916a4aa

Browse files
committed
web: fix TypeScript conflicts with TextEncoder and TextDecoder
In TypeScript, TextEncoder and TextDecoder are global types when targeting the DOM environment, causing conflicts when importing these classes from Node’s util module. To avoid these conflicts, TextEncoder and TextDecoder from util have been imported with different names (NodeTextEncoder, NodeTextDecoder) and assigned to globalThis with explicit type assertions. * MDN - https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder - https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder * StackOverflow - https://stackoverflow.com/a/77752064 * TypeScript types - https://github.com/microsoft/TypeScript/blob/efca03ffed10dccede4fbc8dd8a624374e5424d9/src/lib/dom.generated.d.ts#L32378
1 parent 1878d87 commit 916a4aa

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

web/src/setupTests.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
// expect(element).toHaveTextContent(/react/i)
44
// learn more: https://github.com/testing-library/jest-dom
55
import "@testing-library/jest-dom";
6-
import { TextDecoder, TextEncoder } from "util";
6+
import { TextDecoder as NodeTextDecoder, TextEncoder as NodeTextEncoder } from "util";
77

88
globalThis.IS_REACT_ACT_ENVIRONMENT = true;
99

1010
if (!globalThis.TextEncoder || !globalThis.TextDecoder) {
11-
globalThis.TextEncoder = TextEncoder;
12-
globalThis.TextDecoder = TextDecoder;
11+
globalThis.TextEncoder = NodeTextEncoder as typeof TextEncoder;
12+
globalThis.TextDecoder = NodeTextDecoder as typeof TextDecoder;
1313
}

0 commit comments

Comments
 (0)