-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfutureLike.test-d.ts
74 lines (56 loc) · 3.06 KB
/
futureLike.test-d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { expectAssignable, expectNotAssignable, expectType } from "tsd";
import Future from "./index.js";
const futureLike: Future.Like<string, TypeError> = Promise.resolve("");
const promiseLike: PromiseLike<number> = Promise.resolve(8);
// Expect the FutureLike is not assignable to the PromiseLike with different resolved type.
expectNotAssignable<PromiseLike<boolean>>(futureLike);
// Expect the PromiseLike is not assignable to the FutureLike with different resolved type.
expectNotAssignable<Future.Like<readonly number[], string>>(promiseLike);
// Expect the FutureLike is assignable to the PromiseLike with the same resolved type.
expectAssignable<PromiseLike<string>>(futureLike);
// Expect the PromiseLike is assignable to the FutureLike with the same resolved type.
expectAssignable<Future.Like<number, Error>>(promiseLike);
// Empty then should return of the same FutureLike type.
expectType<Future.Like<string, TypeError>>(futureLike.then());
// Expect the fulfilled type in the then method is the same as in the FutureLike.
futureLike.then((value) => expectType<string>(value));
// Expect the error type in the then is the same as in the FutureLike.
futureLike.then(null, (error) => expectType<TypeError>(error));
// Then with the onfulfilled parameter should return a new fulfilled type
// and the same error type if the callback does not return the FutureLike.
expectType<Future.Like<number, TypeError>>(futureLike.then(() => 8));
// If the onrejected callback returns a non-PromiseLike value, inferred
// error value should be "never".
expectType<Future.Like<string, never>>(futureLike.then(null, () => ""));
// The onrejected callback is allowed to return a different fulfilled
// value from which the main FutureLike holds. The final type will be
// the union of the new type and the futureLike's fulfilled type.
expectType<Future.Like<string | number, never>>(futureLike.then(null, () => 4));
// then method is allowed to return different values from onfulfilled
// and onrejected callbacks simultaneously.
expectType<Future.Like<boolean | string[], never>>(
futureLike.then(
() => false,
() => [""],
),
);
{
const futureLike2: Future.Like<{ readonly foo: string }, number> =
Promise.resolve({ foo: "" });
// If onresolved callback returns another FutureLike, the resulting FutureLike inherits both
// error types and resolved type is changed to the one which is carried by a type returned by
// onresolved callback.
expectType<Future.Like<{ readonly foo: string }, number | TypeError>>(
futureLike.then((_value) => futureLike2),
);
}
// If onresolved callback returns PromiseLike, the error type of the resulting FutureLike
// should be inferred as unknown.
expectType<Future.Like<boolean, unknown>>(
futureLike.then((value) => Promise.resolve(value === "true")),
);
// If onrejected callback returns PromiseLike, the error type of the resulting FutureLike
// should be inferred as unknown and resolved types should be combined.
expectType<Future.Like<string | boolean, unknown>>(
futureLike.then(null, (value) => Promise.resolve(value.name === "TypeError")),
);