From 98f430d665a9673100833b9e4793760cdab53758 Mon Sep 17 00:00:00 2001 From: Daniel Rentz Date: Tue, 14 Jan 2025 10:40:56 +0100 Subject: [PATCH 1/2] fix: #57 more generic types for `Promise.then` and `Promise.catch` --- CONTRIBUTING.md | 12 +++++++++++- docs/diff/es5.d.ts.md | 30 +++++++++++++++--------------- generated/lib.es5.d.ts | 30 +++++++++++++++--------------- lib/lib.es5.d.ts | 30 +++++++++++++++--------------- tests/package-lock.json | 34 +++++++++++++++++----------------- tests/package.json | 34 +++++++++++++++++----------------- tests/src/es5.ts | 14 +++++++++----- 7 files changed, 99 insertions(+), 85 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 7d43e5d..a4374b1 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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. @@ -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` diff --git a/docs/diff/es5.d.ts.md b/docs/diff/es5.d.ts.md index 6ae6c9e..c53c76c 100644 --- a/docs/diff/es5.d.ts.md +++ b/docs/diff/es5.d.ts.md @@ -793,10 +793,10 @@ Index: es5.d.ts - | undefined - | null, - ): PromiseLike; -+ then( -+ onfulfilled?: null | undefined, -+ onrejected?: ((reason: unknown) => T | PromiseLike) | null | undefined, -+ ): PromiseLike; ++ then( ++ onfulfilled: (value: T) => U | PromiseLike, ++ onrejected?: ((reason: unknown) => U | PromiseLike) | null | undefined, ++ ): PromiseLike; + + /** + * Attaches callbacks for the resolution and/or rejection of the Promise. @@ -805,9 +805,9 @@ Index: es5.d.ts + * @returns A Promise for the completion of which ever callback is executed. + */ + then( -+ onfulfilled: (value: T) => U | PromiseLike, ++ onfulfilled: ((value: T) => U | PromiseLike) | null | undefined, + onrejected?: ((reason: unknown) => U | PromiseLike) | null | undefined, -+ ): PromiseLike; ++ ): PromiseLike; } -/** @@ -831,10 +831,10 @@ Index: es5.d.ts - | undefined - | null, - ): Promise; -+ then( -+ onfulfilled?: null | undefined, -+ onrejected?: ((reason: unknown) => T | PromiseLike) | null | undefined, -+ ): Promise; ++ then( ++ onfulfilled: (value: T) => U | PromiseLike, ++ onrejected?: ((reason: unknown) => U | PromiseLike) | null | undefined, ++ ): Promise; /** + * Attaches callbacks for the resolution and/or rejection of the Promise. @@ -843,9 +843,9 @@ Index: es5.d.ts + * @returns A Promise for the completion of which ever callback is executed. + */ + then( -+ onfulfilled: (value: T) => U | PromiseLike, ++ onfulfilled: ((value: T) => U | PromiseLike) | null | undefined, + onrejected?: ((reason: unknown) => U | PromiseLike) | null | undefined, -+ ): Promise; ++ ): Promise; + + /** * Attaches a callback for only the rejection of the Promise. @@ -858,9 +858,9 @@ Index: es5.d.ts - | undefined - | null, - ): Promise; -+ catch( -+ onrejected?: ((reason: unknown) => T | PromiseLike) | null | undefined, -+ ): Promise; ++ catch( ++ onrejected: ((reason: unknown) => U | PromiseLike) | null | undefined, ++ ): Promise; } /** diff --git a/generated/lib.es5.d.ts b/generated/lib.es5.d.ts index 7dc5a5b..dcbc6cc 100644 --- a/generated/lib.es5.d.ts +++ b/generated/lib.es5.d.ts @@ -2095,10 +2095,10 @@ interface PromiseLike { * @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) | null | undefined, - ): PromiseLike; + then( + onfulfilled: (value: T) => U | PromiseLike, + onrejected?: ((reason: unknown) => U | PromiseLike) | null | undefined, + ): PromiseLike; /** * Attaches callbacks for the resolution and/or rejection of the Promise. @@ -2107,9 +2107,9 @@ interface PromiseLike { * @returns A Promise for the completion of which ever callback is executed. */ then( - onfulfilled: (value: T) => U | PromiseLike, + onfulfilled: ((value: T) => U | PromiseLike) | null | undefined, onrejected?: ((reason: unknown) => U | PromiseLike) | null | undefined, - ): PromiseLike; + ): PromiseLike; } // /** // * Attaches callbacks for the resolution and/or rejection of the Promise. @@ -2126,10 +2126,10 @@ interface Promise extends PromiseLike { * @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) | null | undefined, - ): Promise; + then( + onfulfilled: (value: T) => U | PromiseLike, + onrejected?: ((reason: unknown) => U | PromiseLike) | null | undefined, + ): Promise; /** * Attaches callbacks for the resolution and/or rejection of the Promise. @@ -2138,18 +2138,18 @@ interface Promise extends PromiseLike { * @returns A Promise for the completion of which ever callback is executed. */ then( - onfulfilled: (value: T) => U | PromiseLike, + onfulfilled: ((value: T) => U | PromiseLike) | null | undefined, onrejected?: ((reason: unknown) => U | PromiseLike) | null | undefined, - ): Promise; + ): Promise; /** * 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) | null | undefined, - ): Promise; + catch( + onrejected: ((reason: unknown) => U | PromiseLike) | null | undefined, + ): Promise; } // /** // * Represents the completion of an asynchronous operation diff --git a/lib/lib.es5.d.ts b/lib/lib.es5.d.ts index b808a5e..329c95a 100644 --- a/lib/lib.es5.d.ts +++ b/lib/lib.es5.d.ts @@ -587,10 +587,10 @@ interface PromiseLike { * @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) | null | undefined, - ): PromiseLike; + then( + onfulfilled: (value: T) => U | PromiseLike, + onrejected?: ((reason: unknown) => U | PromiseLike) | null | undefined, + ): PromiseLike; /** * Attaches callbacks for the resolution and/or rejection of the Promise. @@ -599,9 +599,9 @@ interface PromiseLike { * @returns A Promise for the completion of which ever callback is executed. */ then( - onfulfilled: (value: T) => U | PromiseLike, + onfulfilled: ((value: T) => U | PromiseLike) | null | undefined, onrejected?: ((reason: unknown) => U | PromiseLike) | null | undefined, - ): PromiseLike; + ): PromiseLike; } interface Promise extends PromiseLike { @@ -611,10 +611,10 @@ interface Promise extends PromiseLike { * @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) | null | undefined, - ): Promise; + then( + onfulfilled: (value: T) => U | PromiseLike, + onrejected?: ((reason: unknown) => U | PromiseLike) | null | undefined, + ): Promise; /** * Attaches callbacks for the resolution and/or rejection of the Promise. @@ -623,18 +623,18 @@ interface Promise extends PromiseLike { * @returns A Promise for the completion of which ever callback is executed. */ then( - onfulfilled: (value: T) => U | PromiseLike, + onfulfilled: ((value: T) => U | PromiseLike) | null | undefined, onrejected?: ((reason: unknown) => U | PromiseLike) | null | undefined, - ): Promise; + ): Promise; /** * 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) | null | undefined, - ): Promise; + catch( + onrejected: ((reason: unknown) => U | PromiseLike) | null | undefined, + ): Promise; } interface TypedNumberArray< diff --git a/tests/package-lock.json b/tests/package-lock.json index 6bca7cc..2a989a1 100644 --- a/tests/package-lock.json +++ b/tests/package-lock.json @@ -8,23 +8,23 @@ "name": "tests", "version": "1.0.0", "dependencies": { - "@typescript/lib-decorators": "file:../dist-package/decorators", - "@typescript/lib-dom": "file:../dist-package/dom", - "@typescript/lib-es2015": "file:../dist-package/es2015", - "@typescript/lib-es2016": "file:../dist-package/es2016", - "@typescript/lib-es2017": "file:../dist-package/es2017", - "@typescript/lib-es2018": "file:../dist-package/es2018", - "@typescript/lib-es2019": "file:../dist-package/es2019", - "@typescript/lib-es2020": "file:../dist-package/es2020", - "@typescript/lib-es2021": "file:../dist-package/es2021", - "@typescript/lib-es2022": "file:../dist-package/es2022", - "@typescript/lib-es2023": "file:../dist-package/es2023", - "@typescript/lib-es2024": "file:../dist-package/es2024", - "@typescript/lib-es5": "file:../dist-package/es5", - "@typescript/lib-es6": "file:../dist-package/es6", - "@typescript/lib-esnext": "file:../dist-package/esnext", - "@typescript/lib-scripthost": "file:../dist-package/scripthost", - "@typescript/lib-webworker": "file:../dist-package/webworker" + "@typescript/lib-decorators": "file:..\\dist-package\\decorators", + "@typescript/lib-dom": "file:..\\dist-package\\dom", + "@typescript/lib-es2015": "file:..\\dist-package\\es2015", + "@typescript/lib-es2016": "file:..\\dist-package\\es2016", + "@typescript/lib-es2017": "file:..\\dist-package\\es2017", + "@typescript/lib-es2018": "file:..\\dist-package\\es2018", + "@typescript/lib-es2019": "file:..\\dist-package\\es2019", + "@typescript/lib-es2020": "file:..\\dist-package\\es2020", + "@typescript/lib-es2021": "file:..\\dist-package\\es2021", + "@typescript/lib-es2022": "file:..\\dist-package\\es2022", + "@typescript/lib-es2023": "file:..\\dist-package\\es2023", + "@typescript/lib-es2024": "file:..\\dist-package\\es2024", + "@typescript/lib-es5": "file:..\\dist-package\\es5", + "@typescript/lib-es6": "file:..\\dist-package\\es6", + "@typescript/lib-esnext": "file:..\\dist-package\\esnext", + "@typescript/lib-scripthost": "file:..\\dist-package\\scripthost", + "@typescript/lib-webworker": "file:..\\dist-package\\webworker" }, "devDependencies": { "@types/node": "^16.9.6", diff --git a/tests/package.json b/tests/package.json index 9278fa5..688f556 100644 --- a/tests/package.json +++ b/tests/package.json @@ -18,23 +18,23 @@ "typescript": "^5.7.2" }, "dependencies": { - "@typescript/lib-decorators": "file:../dist-package/decorators", - "@typescript/lib-dom": "file:../dist-package/dom", - "@typescript/lib-es2015": "file:../dist-package/es2015", - "@typescript/lib-es2016": "file:../dist-package/es2016", - "@typescript/lib-es2017": "file:../dist-package/es2017", - "@typescript/lib-es2018": "file:../dist-package/es2018", - "@typescript/lib-es2019": "file:../dist-package/es2019", - "@typescript/lib-es2020": "file:../dist-package/es2020", - "@typescript/lib-es2021": "file:../dist-package/es2021", - "@typescript/lib-es2022": "file:../dist-package/es2022", - "@typescript/lib-es2023": "file:../dist-package/es2023", - "@typescript/lib-es5": "file:../dist-package/es5", - "@typescript/lib-es6": "file:../dist-package/es6", - "@typescript/lib-esnext": "file:../dist-package/esnext", - "@typescript/lib-scripthost": "file:../dist-package/scripthost", - "@typescript/lib-webworker": "file:../dist-package/webworker", - "@typescript/lib-es2024": "file:../dist-package/es2024" + "@typescript/lib-decorators": "file:..\\dist-package\\decorators", + "@typescript/lib-dom": "file:..\\dist-package\\dom", + "@typescript/lib-es2015": "file:..\\dist-package\\es2015", + "@typescript/lib-es2016": "file:..\\dist-package\\es2016", + "@typescript/lib-es2017": "file:..\\dist-package\\es2017", + "@typescript/lib-es2018": "file:..\\dist-package\\es2018", + "@typescript/lib-es2019": "file:..\\dist-package\\es2019", + "@typescript/lib-es2020": "file:..\\dist-package\\es2020", + "@typescript/lib-es2021": "file:..\\dist-package\\es2021", + "@typescript/lib-es2022": "file:..\\dist-package\\es2022", + "@typescript/lib-es2023": "file:..\\dist-package\\es2023", + "@typescript/lib-es5": "file:..\\dist-package\\es5", + "@typescript/lib-es6": "file:..\\dist-package\\es6", + "@typescript/lib-esnext": "file:..\\dist-package\\esnext", + "@typescript/lib-scripthost": "file:..\\dist-package\\scripthost", + "@typescript/lib-webworker": "file:..\\dist-package\\webworker", + "@typescript/lib-es2024": "file:..\\dist-package\\es2024" }, "overrides": { "@tsd/typescript": "~5.7.2" diff --git a/tests/src/es5.ts b/tests/src/es5.ts index cf34e39..37a8e66 100644 --- a/tests/src/es5.ts +++ b/tests/src/es5.ts @@ -39,8 +39,6 @@ const testPromiseConstructorLike = (MyPromise: PromiseConstructorLike) => { }; // Promise const testPromise = (promise: Promise) => { - expectType>(promise.then()); - expectType>(promise.catch()); expectType>(promise.then(null)); expectType>(promise.then(undefined)); expectType>(promise.catch(null)); @@ -70,6 +68,14 @@ const testPromise = (promise: Promise) => { (err) => Promise.resolve(`${err}`.length), ), ); + expectType>(promise.then(() => 42, () => 42)); + expectType>(promise.then(null, () => 42)); + expectType>(promise.then(undefined, () => 42)); + expectType>(promise.catch(() => 42)); + // @ts-expect-error + promise.then(); + // @ts-expect-error + promise.catch(); // @ts-expect-error promise.then((str: string) => str); promise.then( @@ -78,8 +84,6 @@ const testPromise = (promise: Promise) => { () => "NaN", ); // @ts-expect-error - promise.then(null, (err) => `${err}`.length); - // @ts-expect-error promise.catch(null, (err) => `${err}`.length); }; @@ -525,4 +529,4 @@ expectType<{ foo: number; bar: string; baz: boolean }>( } } -export {}; +export { }; From 3ff4b2ec24009939e9693cc76469e98941ef2125 Mon Sep 17 00:00:00 2001 From: Daniel Rentz Date: Tue, 14 Jan 2025 11:00:31 +0100 Subject: [PATCH 2/2] fix wrong windows paths in package.json --- tests/package-lock.json | 34 +++++++++++++++++----------------- tests/package.json | 34 +++++++++++++++++----------------- 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/tests/package-lock.json b/tests/package-lock.json index 2a989a1..6bca7cc 100644 --- a/tests/package-lock.json +++ b/tests/package-lock.json @@ -8,23 +8,23 @@ "name": "tests", "version": "1.0.0", "dependencies": { - "@typescript/lib-decorators": "file:..\\dist-package\\decorators", - "@typescript/lib-dom": "file:..\\dist-package\\dom", - "@typescript/lib-es2015": "file:..\\dist-package\\es2015", - "@typescript/lib-es2016": "file:..\\dist-package\\es2016", - "@typescript/lib-es2017": "file:..\\dist-package\\es2017", - "@typescript/lib-es2018": "file:..\\dist-package\\es2018", - "@typescript/lib-es2019": "file:..\\dist-package\\es2019", - "@typescript/lib-es2020": "file:..\\dist-package\\es2020", - "@typescript/lib-es2021": "file:..\\dist-package\\es2021", - "@typescript/lib-es2022": "file:..\\dist-package\\es2022", - "@typescript/lib-es2023": "file:..\\dist-package\\es2023", - "@typescript/lib-es2024": "file:..\\dist-package\\es2024", - "@typescript/lib-es5": "file:..\\dist-package\\es5", - "@typescript/lib-es6": "file:..\\dist-package\\es6", - "@typescript/lib-esnext": "file:..\\dist-package\\esnext", - "@typescript/lib-scripthost": "file:..\\dist-package\\scripthost", - "@typescript/lib-webworker": "file:..\\dist-package\\webworker" + "@typescript/lib-decorators": "file:../dist-package/decorators", + "@typescript/lib-dom": "file:../dist-package/dom", + "@typescript/lib-es2015": "file:../dist-package/es2015", + "@typescript/lib-es2016": "file:../dist-package/es2016", + "@typescript/lib-es2017": "file:../dist-package/es2017", + "@typescript/lib-es2018": "file:../dist-package/es2018", + "@typescript/lib-es2019": "file:../dist-package/es2019", + "@typescript/lib-es2020": "file:../dist-package/es2020", + "@typescript/lib-es2021": "file:../dist-package/es2021", + "@typescript/lib-es2022": "file:../dist-package/es2022", + "@typescript/lib-es2023": "file:../dist-package/es2023", + "@typescript/lib-es2024": "file:../dist-package/es2024", + "@typescript/lib-es5": "file:../dist-package/es5", + "@typescript/lib-es6": "file:../dist-package/es6", + "@typescript/lib-esnext": "file:../dist-package/esnext", + "@typescript/lib-scripthost": "file:../dist-package/scripthost", + "@typescript/lib-webworker": "file:../dist-package/webworker" }, "devDependencies": { "@types/node": "^16.9.6", diff --git a/tests/package.json b/tests/package.json index 688f556..9278fa5 100644 --- a/tests/package.json +++ b/tests/package.json @@ -18,23 +18,23 @@ "typescript": "^5.7.2" }, "dependencies": { - "@typescript/lib-decorators": "file:..\\dist-package\\decorators", - "@typescript/lib-dom": "file:..\\dist-package\\dom", - "@typescript/lib-es2015": "file:..\\dist-package\\es2015", - "@typescript/lib-es2016": "file:..\\dist-package\\es2016", - "@typescript/lib-es2017": "file:..\\dist-package\\es2017", - "@typescript/lib-es2018": "file:..\\dist-package\\es2018", - "@typescript/lib-es2019": "file:..\\dist-package\\es2019", - "@typescript/lib-es2020": "file:..\\dist-package\\es2020", - "@typescript/lib-es2021": "file:..\\dist-package\\es2021", - "@typescript/lib-es2022": "file:..\\dist-package\\es2022", - "@typescript/lib-es2023": "file:..\\dist-package\\es2023", - "@typescript/lib-es5": "file:..\\dist-package\\es5", - "@typescript/lib-es6": "file:..\\dist-package\\es6", - "@typescript/lib-esnext": "file:..\\dist-package\\esnext", - "@typescript/lib-scripthost": "file:..\\dist-package\\scripthost", - "@typescript/lib-webworker": "file:..\\dist-package\\webworker", - "@typescript/lib-es2024": "file:..\\dist-package\\es2024" + "@typescript/lib-decorators": "file:../dist-package/decorators", + "@typescript/lib-dom": "file:../dist-package/dom", + "@typescript/lib-es2015": "file:../dist-package/es2015", + "@typescript/lib-es2016": "file:../dist-package/es2016", + "@typescript/lib-es2017": "file:../dist-package/es2017", + "@typescript/lib-es2018": "file:../dist-package/es2018", + "@typescript/lib-es2019": "file:../dist-package/es2019", + "@typescript/lib-es2020": "file:../dist-package/es2020", + "@typescript/lib-es2021": "file:../dist-package/es2021", + "@typescript/lib-es2022": "file:../dist-package/es2022", + "@typescript/lib-es2023": "file:../dist-package/es2023", + "@typescript/lib-es5": "file:../dist-package/es5", + "@typescript/lib-es6": "file:../dist-package/es6", + "@typescript/lib-esnext": "file:../dist-package/esnext", + "@typescript/lib-scripthost": "file:../dist-package/scripthost", + "@typescript/lib-webworker": "file:../dist-package/webworker", + "@typescript/lib-es2024": "file:../dist-package/es2024" }, "overrides": { "@tsd/typescript": "~5.7.2"