Skip to content

Commit 915eefd

Browse files
committed
sync(common-v8): tighten Promise polyfill typing for TS 5.9
Adjust Promise.withResolvers/Promise.try assignments to pass current TypeScript checks without non-null or ts-expect-error hacks.
1 parent db68e41 commit 915eefd

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

packages/react-native/src/Polyfills.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,20 @@ export const installPolyfills = (): void => {
2323
};
2424

2525
const installPromisePolyfills = () => {
26+
const PromiseStatic = Promise as PromiseConstructor & {
27+
withResolvers?: <T>() => PromiseWithResolvers<T>;
28+
try?: (
29+
func: (...args: ReadonlyArray<unknown>) => unknown,
30+
...args: ReadonlyArray<unknown>
31+
) => Promise<unknown>;
32+
};
33+
2634
// @see https://github.com/facebook/hermes/pull/1452
27-
if (typeof Promise.withResolvers !== "function") {
28-
// @ts-expect-error This is OK.
29-
Promise.withResolvers = () => {
30-
let resolve, reject;
31-
const promise = new Promise((res, rej) => {
35+
if (typeof PromiseStatic.withResolvers !== "function") {
36+
PromiseStatic.withResolvers = <T>() => {
37+
let resolve: (value: T | PromiseLike<T>) => void = () => {};
38+
let reject: (reason?: unknown) => void = () => {};
39+
const promise = new Promise<T>((res, rej) => {
3240
resolve = res;
3341
reject = rej;
3442
});
@@ -37,9 +45,8 @@ const installPromisePolyfills = () => {
3745
}
3846

3947
// @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/try
40-
if (typeof Promise.try !== "function") {
41-
// @ts-expect-error This is OK.
42-
Promise.try = (
48+
if (typeof PromiseStatic.try !== "function") {
49+
PromiseStatic.try = (
4350
func: (...args: ReadonlyArray<unknown>) => unknown,
4451
...args: ReadonlyArray<unknown>
4552
): Promise<unknown> =>

0 commit comments

Comments
 (0)