Skip to content

Commit 222baac

Browse files
authored
fix: #57 more generic types for Promise.then and Promise.catch (#60)
* fix: #57 more generic types for `Promise.then` and `Promise.catch` * fix wrong windows paths in package.json
1 parent 22a8267 commit 222baac

File tree

5 files changed

+65
-51
lines changed

5 files changed

+65
-51
lines changed

CONTRIBUTING.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ Feature suggestions are welcome, but please be aware that:
2020

2121
Below are the guidelines for submitting a pull request.
2222

23+
### Preparations
24+
25+
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`.
26+
27+
In the cloned repository:
28+
29+
1. `git submodule init`
30+
2. `git submodule update` (downloading the 3 GB sized TypeScript repository will take a while ...)
31+
3. `npm install`
32+
2333
### How to change type definitions
2434

2535
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
4959

5060
### Committing Build Artifacts
5161

52-
Currently, build artifacts needs to be committed. Follow the following steps to build and commit them.
62+
Currently, build artifacts need to be committed. Follow the steps below to build and commit them.
5363

5464
1. `npm run build:tsc`
5565
2. `npm run build:lib`

docs/diff/es5.d.ts.md

+15-15
Original file line numberDiff line numberDiff line change
@@ -793,10 +793,10 @@ Index: es5.d.ts
793793
- | undefined
794794
- | null,
795795
- ): PromiseLike<TResult1 | TResult2>;
796-
+ then(
797-
+ onfulfilled?: null | undefined,
798-
+ onrejected?: ((reason: unknown) => T | PromiseLike<T>) | null | undefined,
799-
+ ): PromiseLike<T>;
796+
+ then<U>(
797+
+ onfulfilled: (value: T) => U | PromiseLike<U>,
798+
+ onrejected?: ((reason: unknown) => U | PromiseLike<U>) | null | undefined,
799+
+ ): PromiseLike<U>;
800800
+
801801
+ /**
802802
+ * Attaches callbacks for the resolution and/or rejection of the Promise.
@@ -805,9 +805,9 @@ Index: es5.d.ts
805805
+ * @returns A Promise for the completion of which ever callback is executed.
806806
+ */
807807
+ then<U>(
808-
+ onfulfilled: (value: T) => U | PromiseLike<U>,
808+
+ onfulfilled: ((value: T) => U | PromiseLike<U>) | null | undefined,
809809
+ onrejected?: ((reason: unknown) => U | PromiseLike<U>) | null | undefined,
810-
+ ): PromiseLike<U>;
810+
+ ): PromiseLike<T | U>;
811811
}
812812

813813
-/**
@@ -831,10 +831,10 @@ Index: es5.d.ts
831831
- | undefined
832832
- | null,
833833
- ): Promise<TResult1 | TResult2>;
834-
+ then(
835-
+ onfulfilled?: null | undefined,
836-
+ onrejected?: ((reason: unknown) => T | PromiseLike<T>) | null | undefined,
837-
+ ): Promise<T>;
834+
+ then<U>(
835+
+ onfulfilled: (value: T) => U | PromiseLike<U>,
836+
+ onrejected?: ((reason: unknown) => U | PromiseLike<U>) | null | undefined,
837+
+ ): Promise<U>;
838838

839839
/**
840840
+ * Attaches callbacks for the resolution and/or rejection of the Promise.
@@ -843,9 +843,9 @@ Index: es5.d.ts
843843
+ * @returns A Promise for the completion of which ever callback is executed.
844844
+ */
845845
+ then<U>(
846-
+ onfulfilled: (value: T) => U | PromiseLike<U>,
846+
+ onfulfilled: ((value: T) => U | PromiseLike<U>) | null | undefined,
847847
+ onrejected?: ((reason: unknown) => U | PromiseLike<U>) | null | undefined,
848-
+ ): Promise<U>;
848+
+ ): Promise<T | U>;
849849
+
850850
+ /**
851851
* Attaches a callback for only the rejection of the Promise.
@@ -858,9 +858,9 @@ Index: es5.d.ts
858858
- | undefined
859859
- | null,
860860
- ): Promise<T | TResult>;
861-
+ catch(
862-
+ onrejected?: ((reason: unknown) => T | PromiseLike<T>) | null | undefined,
863-
+ ): Promise<T>;
861+
+ catch<U>(
862+
+ onrejected: ((reason: unknown) => U | PromiseLike<U>) | null | undefined,
863+
+ ): Promise<T | U>;
864864
}
865865

866866
/**

generated/lib.es5.d.ts

+15-15
Original file line numberDiff line numberDiff line change
@@ -2095,10 +2095,10 @@ interface PromiseLike<T> {
20952095
* @param onrejected The callback to execute when the Promise is rejected.
20962096
* @returns A Promise for the completion of which ever callback is executed.
20972097
*/
2098-
then(
2099-
onfulfilled?: null | undefined,
2100-
onrejected?: ((reason: unknown) => T | PromiseLike<T>) | null | undefined,
2101-
): PromiseLike<T>;
2098+
then<U>(
2099+
onfulfilled: (value: T) => U | PromiseLike<U>,
2100+
onrejected?: ((reason: unknown) => U | PromiseLike<U>) | null | undefined,
2101+
): PromiseLike<U>;
21022102

21032103
/**
21042104
* Attaches callbacks for the resolution and/or rejection of the Promise.
@@ -2107,9 +2107,9 @@ interface PromiseLike<T> {
21072107
* @returns A Promise for the completion of which ever callback is executed.
21082108
*/
21092109
then<U>(
2110-
onfulfilled: (value: T) => U | PromiseLike<U>,
2110+
onfulfilled: ((value: T) => U | PromiseLike<U>) | null | undefined,
21112111
onrejected?: ((reason: unknown) => U | PromiseLike<U>) | null | undefined,
2112-
): PromiseLike<U>;
2112+
): PromiseLike<T | U>;
21132113
}
21142114
// /**
21152115
// * Attaches callbacks for the resolution and/or rejection of the Promise.
@@ -2126,10 +2126,10 @@ interface Promise<T> extends PromiseLike<T> {
21262126
* @param onrejected The callback to execute when the Promise is rejected.
21272127
* @returns A Promise for the completion of which ever callback is executed.
21282128
*/
2129-
then(
2130-
onfulfilled?: null | undefined,
2131-
onrejected?: ((reason: unknown) => T | PromiseLike<T>) | null | undefined,
2132-
): Promise<T>;
2129+
then<U>(
2130+
onfulfilled: (value: T) => U | PromiseLike<U>,
2131+
onrejected?: ((reason: unknown) => U | PromiseLike<U>) | null | undefined,
2132+
): Promise<U>;
21332133

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

21452145
/**
21462146
* Attaches a callback for only the rejection of the Promise.
21472147
* @param onrejected The callback to execute when the Promise is rejected.
21482148
* @returns A Promise for the completion of the callback.
21492149
*/
2150-
catch(
2151-
onrejected?: ((reason: unknown) => T | PromiseLike<T>) | null | undefined,
2152-
): Promise<T>;
2150+
catch<U>(
2151+
onrejected: ((reason: unknown) => U | PromiseLike<U>) | null | undefined,
2152+
): Promise<T | U>;
21532153
}
21542154
// /**
21552155
// * Represents the completion of an asynchronous operation

lib/lib.es5.d.ts

+15-15
Original file line numberDiff line numberDiff line change
@@ -587,10 +587,10 @@ interface PromiseLike<T> {
587587
* @param onrejected The callback to execute when the Promise is rejected.
588588
* @returns A Promise for the completion of which ever callback is executed.
589589
*/
590-
then(
591-
onfulfilled?: null | undefined,
592-
onrejected?: ((reason: unknown) => T | PromiseLike<T>) | null | undefined,
593-
): PromiseLike<T>;
590+
then<U>(
591+
onfulfilled: (value: T) => U | PromiseLike<U>,
592+
onrejected?: ((reason: unknown) => U | PromiseLike<U>) | null | undefined,
593+
): PromiseLike<U>;
594594

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

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

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

630630
/**
631631
* Attaches a callback for only the rejection of the Promise.
632632
* @param onrejected The callback to execute when the Promise is rejected.
633633
* @returns A Promise for the completion of the callback.
634634
*/
635-
catch(
636-
onrejected?: ((reason: unknown) => T | PromiseLike<T>) | null | undefined,
637-
): Promise<T>;
635+
catch<U>(
636+
onrejected: ((reason: unknown) => U | PromiseLike<U>) | null | undefined,
637+
): Promise<T | U>;
638638
}
639639

640640
interface TypedNumberArray<

tests/src/es5.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ const testPromiseConstructorLike = (MyPromise: PromiseConstructorLike) => {
3939
};
4040
// Promise
4141
const testPromise = (promise: Promise<string>) => {
42-
expectType<Promise<string>>(promise.then());
43-
expectType<Promise<string>>(promise.catch());
4442
expectType<Promise<string>>(promise.then(null));
4543
expectType<Promise<string>>(promise.then(undefined));
4644
expectType<Promise<string>>(promise.catch(null));
@@ -70,6 +68,14 @@ const testPromise = (promise: Promise<string>) => {
7068
(err) => Promise.resolve(`${err}`.length),
7169
),
7270
);
71+
expectType<Promise<number>>(promise.then(() => 42, () => 42));
72+
expectType<Promise<string | number>>(promise.then(null, () => 42));
73+
expectType<Promise<string | number>>(promise.then(undefined, () => 42));
74+
expectType<Promise<string | number>>(promise.catch(() => 42));
75+
// @ts-expect-error
76+
promise.then();
77+
// @ts-expect-error
78+
promise.catch();
7379
// @ts-expect-error
7480
promise.then<number>((str: string) => str);
7581
promise.then<number>(
@@ -78,8 +84,6 @@ const testPromise = (promise: Promise<string>) => {
7884
() => "NaN",
7985
);
8086
// @ts-expect-error
81-
promise.then(null, (err) => `${err}`.length);
82-
// @ts-expect-error
8387
promise.catch(null, (err) => `${err}`.length);
8488
};
8589

@@ -525,4 +529,4 @@ expectType<{ foo: number; bar: string; baz: boolean }>(
525529
}
526530
}
527531

528-
export {};
532+
export { };

0 commit comments

Comments
 (0)