Skip to content

fix: #57 more generic types for Promise.then and Promise.catch #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ Feature suggestions are welcome, but please be aware that:

Below are the guidelines for submitting a pull request.

### Preparations

If you are working on Windows, you may need to enable support for long paths in Git, othwise cloning the submodule may fail. Open an administrator shell, and execute `git config --system core.longpaths true`.

In the cloned repository:

1. `git submodule init`
2. `git submodule update` (downloading the 3 GB sized TypeScript repository will take a while ...)
3. `npm install`

### How to change type definitions

better-typescript-lib replaces the built-in type definitions with its own ones. The renewed definitions are in the `lib/` directory.
Expand Down Expand Up @@ -49,7 +59,7 @@ For other declarations, such as type aliases, enums, and namespaces, the replace

### Committing Build Artifacts

Currently, build artifacts needs to be committed. Follow the following steps to build and commit them.
Currently, build artifacts need to be committed. Follow the steps below to build and commit them.

1. `npm run build:tsc`
2. `npm run build:lib`
Expand Down
30 changes: 15 additions & 15 deletions docs/diff/es5.d.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -793,10 +793,10 @@ Index: es5.d.ts
- | undefined
- | null,
- ): PromiseLike<TResult1 | TResult2>;
+ then(
+ onfulfilled?: null | undefined,
+ onrejected?: ((reason: unknown) => T | PromiseLike<T>) | null | undefined,
+ ): PromiseLike<T>;
+ then<U>(
+ onfulfilled: (value: T) => U | PromiseLike<U>,
+ onrejected?: ((reason: unknown) => U | PromiseLike<U>) | null | undefined,
+ ): PromiseLike<U>;
+
+ /**
+ * Attaches callbacks for the resolution and/or rejection of the Promise.
Expand All @@ -805,9 +805,9 @@ Index: es5.d.ts
+ * @returns A Promise for the completion of which ever callback is executed.
+ */
+ then<U>(
+ onfulfilled: (value: T) => U | PromiseLike<U>,
+ onfulfilled: ((value: T) => U | PromiseLike<U>) | null | undefined,
+ onrejected?: ((reason: unknown) => U | PromiseLike<U>) | null | undefined,
+ ): PromiseLike<U>;
+ ): PromiseLike<T | U>;
}

-/**
Expand All @@ -831,10 +831,10 @@ Index: es5.d.ts
- | undefined
- | null,
- ): Promise<TResult1 | TResult2>;
+ then(
+ onfulfilled?: null | undefined,
+ onrejected?: ((reason: unknown) => T | PromiseLike<T>) | null | undefined,
+ ): Promise<T>;
+ then<U>(
+ onfulfilled: (value: T) => U | PromiseLike<U>,
+ onrejected?: ((reason: unknown) => U | PromiseLike<U>) | null | undefined,
+ ): Promise<U>;

/**
+ * Attaches callbacks for the resolution and/or rejection of the Promise.
Expand All @@ -843,9 +843,9 @@ Index: es5.d.ts
+ * @returns A Promise for the completion of which ever callback is executed.
+ */
+ then<U>(
+ onfulfilled: (value: T) => U | PromiseLike<U>,
+ onfulfilled: ((value: T) => U | PromiseLike<U>) | null | undefined,
+ onrejected?: ((reason: unknown) => U | PromiseLike<U>) | null | undefined,
+ ): Promise<U>;
+ ): Promise<T | U>;
+
+ /**
* Attaches a callback for only the rejection of the Promise.
Expand All @@ -858,9 +858,9 @@ Index: es5.d.ts
- | undefined
- | null,
- ): Promise<T | TResult>;
+ catch(
+ onrejected?: ((reason: unknown) => T | PromiseLike<T>) | null | undefined,
+ ): Promise<T>;
+ catch<U>(
+ onrejected: ((reason: unknown) => U | PromiseLike<U>) | null | undefined,
+ ): Promise<T | U>;
}

/**
Expand Down
30 changes: 15 additions & 15 deletions generated/lib.es5.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2095,10 +2095,10 @@ interface PromiseLike<T> {
* @param onrejected The callback to execute when the Promise is rejected.
* @returns A Promise for the completion of which ever callback is executed.
*/
then(
onfulfilled?: null | undefined,
onrejected?: ((reason: unknown) => T | PromiseLike<T>) | null | undefined,
): PromiseLike<T>;
then<U>(
onfulfilled: (value: T) => U | PromiseLike<U>,
onrejected?: ((reason: unknown) => U | PromiseLike<U>) | null | undefined,
): PromiseLike<U>;

/**
* Attaches callbacks for the resolution and/or rejection of the Promise.
Expand All @@ -2107,9 +2107,9 @@ interface PromiseLike<T> {
* @returns A Promise for the completion of which ever callback is executed.
*/
then<U>(
onfulfilled: (value: T) => U | PromiseLike<U>,
onfulfilled: ((value: T) => U | PromiseLike<U>) | null | undefined,
onrejected?: ((reason: unknown) => U | PromiseLike<U>) | null | undefined,
): PromiseLike<U>;
): PromiseLike<T | U>;
}
// /**
// * Attaches callbacks for the resolution and/or rejection of the Promise.
Expand All @@ -2126,10 +2126,10 @@ interface Promise<T> extends PromiseLike<T> {
* @param onrejected The callback to execute when the Promise is rejected.
* @returns A Promise for the completion of which ever callback is executed.
*/
then(
onfulfilled?: null | undefined,
onrejected?: ((reason: unknown) => T | PromiseLike<T>) | null | undefined,
): Promise<T>;
then<U>(
onfulfilled: (value: T) => U | PromiseLike<U>,
onrejected?: ((reason: unknown) => U | PromiseLike<U>) | null | undefined,
): Promise<U>;

/**
* Attaches callbacks for the resolution and/or rejection of the Promise.
Expand All @@ -2138,18 +2138,18 @@ interface Promise<T> extends PromiseLike<T> {
* @returns A Promise for the completion of which ever callback is executed.
*/
then<U>(
onfulfilled: (value: T) => U | PromiseLike<U>,
onfulfilled: ((value: T) => U | PromiseLike<U>) | null | undefined,
onrejected?: ((reason: unknown) => U | PromiseLike<U>) | null | undefined,
): Promise<U>;
): Promise<T | U>;

/**
* Attaches a callback for only the rejection of the Promise.
* @param onrejected The callback to execute when the Promise is rejected.
* @returns A Promise for the completion of the callback.
*/
catch(
onrejected?: ((reason: unknown) => T | PromiseLike<T>) | null | undefined,
): Promise<T>;
catch<U>(
onrejected: ((reason: unknown) => U | PromiseLike<U>) | null | undefined,
): Promise<T | U>;
}
// /**
// * Represents the completion of an asynchronous operation
Expand Down
30 changes: 15 additions & 15 deletions lib/lib.es5.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -587,10 +587,10 @@ interface PromiseLike<T> {
* @param onrejected The callback to execute when the Promise is rejected.
* @returns A Promise for the completion of which ever callback is executed.
*/
then(
onfulfilled?: null | undefined,
onrejected?: ((reason: unknown) => T | PromiseLike<T>) | null | undefined,
): PromiseLike<T>;
then<U>(
onfulfilled: (value: T) => U | PromiseLike<U>,
onrejected?: ((reason: unknown) => U | PromiseLike<U>) | null | undefined,
): PromiseLike<U>;

/**
* Attaches callbacks for the resolution and/or rejection of the Promise.
Expand All @@ -599,9 +599,9 @@ interface PromiseLike<T> {
* @returns A Promise for the completion of which ever callback is executed.
*/
then<U>(
onfulfilled: (value: T) => U | PromiseLike<U>,
onfulfilled: ((value: T) => U | PromiseLike<U>) | null | undefined,
onrejected?: ((reason: unknown) => U | PromiseLike<U>) | null | undefined,
): PromiseLike<U>;
): PromiseLike<T | U>;
}

interface Promise<T> extends PromiseLike<T> {
Expand All @@ -611,10 +611,10 @@ interface Promise<T> extends PromiseLike<T> {
* @param onrejected The callback to execute when the Promise is rejected.
* @returns A Promise for the completion of which ever callback is executed.
*/
then(
onfulfilled?: null | undefined,
onrejected?: ((reason: unknown) => T | PromiseLike<T>) | null | undefined,
): Promise<T>;
then<U>(
onfulfilled: (value: T) => U | PromiseLike<U>,
onrejected?: ((reason: unknown) => U | PromiseLike<U>) | null | undefined,
): Promise<U>;

/**
* Attaches callbacks for the resolution and/or rejection of the Promise.
Expand All @@ -623,18 +623,18 @@ interface Promise<T> extends PromiseLike<T> {
* @returns A Promise for the completion of which ever callback is executed.
*/
then<U>(
onfulfilled: (value: T) => U | PromiseLike<U>,
onfulfilled: ((value: T) => U | PromiseLike<U>) | null | undefined,
onrejected?: ((reason: unknown) => U | PromiseLike<U>) | null | undefined,
): Promise<U>;
): Promise<T | U>;

/**
* Attaches a callback for only the rejection of the Promise.
* @param onrejected The callback to execute when the Promise is rejected.
* @returns A Promise for the completion of the callback.
*/
catch(
onrejected?: ((reason: unknown) => T | PromiseLike<T>) | null | undefined,
): Promise<T>;
catch<U>(
onrejected: ((reason: unknown) => U | PromiseLike<U>) | null | undefined,
): Promise<T | U>;
}

interface TypedNumberArray<
Expand Down
14 changes: 9 additions & 5 deletions tests/src/es5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ const testPromiseConstructorLike = (MyPromise: PromiseConstructorLike) => {
};
// Promise
const testPromise = (promise: Promise<string>) => {
expectType<Promise<string>>(promise.then());
expectType<Promise<string>>(promise.catch());
expectType<Promise<string>>(promise.then(null));
expectType<Promise<string>>(promise.then(undefined));
expectType<Promise<string>>(promise.catch(null));
Expand Down Expand Up @@ -70,6 +68,14 @@ const testPromise = (promise: Promise<string>) => {
(err) => Promise.resolve(`${err}`.length),
),
);
expectType<Promise<number>>(promise.then(() => 42, () => 42));
expectType<Promise<string | number>>(promise.then(null, () => 42));
expectType<Promise<string | number>>(promise.then(undefined, () => 42));
expectType<Promise<string | number>>(promise.catch(() => 42));
// @ts-expect-error
promise.then();
// @ts-expect-error
promise.catch();
// @ts-expect-error
promise.then<number>((str: string) => str);
promise.then<number>(
Expand All @@ -78,8 +84,6 @@ const testPromise = (promise: Promise<string>) => {
() => "NaN",
);
// @ts-expect-error
promise.then(null, (err) => `${err}`.length);
// @ts-expect-error
promise.catch(null, (err) => `${err}`.length);
};

Expand Down Expand Up @@ -525,4 +529,4 @@ expectType<{ foo: number; bar: string; baz: boolean }>(
}
}

export {};
export { };
Loading