File: awaitedType.errors.txt

package info (click to toggle)
node-typescript 5.1.6%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 492,516 kB
  • sloc: javascript: 2,078,951; makefile: 6; sh: 1
file content (264 lines) | stat: -rw-r--r-- 9,556 bytes parent folder | download
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
tests/cases/compiler/awaitedType.ts(22,12): error TS2589: Type instantiation is excessively deep and possibly infinite.
tests/cases/compiler/awaitedType.ts(26,12): error TS2589: Type instantiation is excessively deep and possibly infinite.
tests/cases/compiler/awaitedType.ts(236,28): error TS2493: Tuple type '[number]' of length '1' has no element at index '1'.


==== tests/cases/compiler/awaitedType.ts (3 errors) ====
    type T1 = Awaited<number>;
    type T2 = Awaited<Promise<number>>;
    type T3 = Awaited<number | Promise<number>>;
    type T4 = Awaited<number | Promise<string>>;
    type T5 = Awaited<{ then: number }>;
    type T6 = Awaited<{ then(): void }>; // never (non-promise "thenable")
    type T7 = Awaited<{ then(x: number): void }>; // never (non-promise "thenable")
    type T8 = Awaited<{ then(x: () => void): void }>; // unknown
    type T9 = Awaited<any>;
    type T10 = Awaited<never>;
    type T11 = Awaited<unknown>;
    type T12 = Awaited<Promise<Promise<number>>>;
    type T13 = _Expect<Awaited<Promise<Promise<number>> | string | null>, /*expected*/ string | number | null>; // otherwise just prints T13 in types tests, which isn't very helpful
    type T14 = _Expect<Awaited<Promise<Promise<number>> | string | undefined>, /*expected*/ string | number | undefined>; // otherwise just prints T14 in types tests, which isn't very helpful
    type T15 = _Expect<Awaited<Promise<Promise<number>> | string | null | undefined>, /*expected*/ string | number | null | undefined>; // otherwise just prints T15 in types tests, which isn't very helpful
    
    type TUndefined = Awaited<undefined>;
    type TNull = Awaited<null>;
    type TNullOrUndefined = Awaited<null | undefined>;
    
    interface BadPromise { then(cb: (value: BadPromise) => void): void; }
    type T16 = Awaited<BadPromise>; // error
               ~~~~~~~~~~~~~~~~~~~
!!! error TS2589: Type instantiation is excessively deep and possibly infinite.
    
    interface BadPromise1 { then(cb: (value: BadPromise2) => void): void; }
    interface BadPromise2 { then(cb: (value: BadPromise1) => void): void; }
    type T17 = Awaited<BadPromise1>; // error
               ~~~~~~~~~~~~~~~~~~~~
!!! error TS2589: Type instantiation is excessively deep and possibly infinite.
    
    // https://github.com/microsoft/TypeScript/issues/46934
    type T18 = Awaited<{ then(cb: (value: number, other: { }) => void)}>; // number
    
    // https://github.com/microsoft/TypeScript/issues/33562
    type MaybePromise<T> = T | Promise<T> | PromiseLike<T>
    declare function MaybePromise<T>(value: T): MaybePromise<T>;
    
    async function main() {
        let aaa: number;
        let bbb: string;
        [
            aaa,
            bbb,
        ] = await Promise.all([
            MaybePromise(1),
            MaybePromise('2'),
            MaybePromise(true),
        ])
    }
    
    // non-generic
    async function f1(x: string) {
        // y: string
        const y = await x;
    }
    
    async function f2(x: unknown) {
        // y: unknown
        const y = await x;
    }
    
    async function f3(x: object) {
        // y: object
        const y = await x;
    }
    
    async function f4(x: Promise<string>) {
        // y: string
        const y = await x;
    }
    
    async function f5(x: Promise<unknown>) {
        // y: unknown
        const y = await x;
    }
    
    async function f6(x: Promise<object>) {
        // y: object
        const y = await x;
    }
    
    // generic
    
    async function f7<T>(x: T) {
        // NOTE: T does not belong solely to the domain of primitive types and either does
        // not have a base constraint, its base constraint is `any`, `unknown`, `{}`, or `object`,
        // or it has a non-primitive base constraint with a callable `then`.
    
        // y: Awaited<T>
        const y = await x;
    }
    
    async function f8<T extends any>(x: T) {
        // NOTE: T does not belong solely to the domain of primitive types and either does
        // not have a base constraint, its base constraint is `any`, `unknown`, `{}`, or `object`,
        // or it has a non-primitive base constraint with a callable `then`.
    
        // y: Awaited<T>
        const y = await x;
    }
    
    async function f9<T extends unknown>(x: T) {
        // NOTE: T does not belong solely to the domain of primitive types and either does
        // not have a base constraint, its base constraint is `any`, `unknown`, `{}`, or `object`,
        // or it has a non-primitive base constraint with a callable `then`.
    
        // y: Awaited<T>
        const y = await x;
    }
    
    async function f10<T extends {}>(x: T) {
        // NOTE: T does not belong solely to the domain of primitive types and either does
        // not have a base constraint, its base constraint is `any`, `unknown`, `{}`, or `object`,
        // or it has a non-primitive base constraint with a callable `then`.
    
        // y: Awaited<T>
        const y = await x;
    }
    
    async function f11<T extends { then(onfulfilled: (value: unknown) => void): void }>(x: T) {
        // NOTE: T does not belong solely to the domain of primitive types and either does
        // not have a base constraint, its base constraint is `any`, `unknown`, `{}`, or `object`,
        // or it has a non-primitive base constraint with a callable `then`.
    
        // y: Awaited<T>
        const y = await x;
    }
    
    async function f12<T extends string | object>(x: T) {
        // NOTE: T does not belong solely to the domain of primitive types and either does
        // not have a base constraint, its base constraint is `any`, `unknown`, `{}`, or `object`,
        // or it has a non-primitive base constraint with a callable `then`.
    
        // y: Awaited<T>
        const y = await x;
    }
    
    async function f13<T extends string>(x: T) {
        // NOTE: T belongs to the domain of primitive types
    
        // y: T
        const y = await x;
    }
    
    async function f14<T extends { x: number }>(x: T) {
        // NOTE: T has a non-primitive base constraint without a callable `then`.
    
        // y: T
        const y = await x;
    }
    
    async function f15<T extends { then: number }>(x: T) {
        // NOTE: T has a non-primitive base constraint without a callable `then`.
    
        // y: T
        const y = await x;
    }
    
    async function f16<T extends number & { then(): void }>(x: T) {
        // NOTE: T belongs to the domain of primitive types (regardless of `then`)
    
        // y: T
        const y = await x;
    }
    
    
    // helps with tests where '.types' just prints out the type alias name
    type _Expect<TActual extends TExpected, TExpected> = TActual;
    
    // https://github.com/microsoft/TypeScript/issues/48320
    async function f17<T extends (...args: any[]) => Promise<any>>(fn: T) {
        const ret: Awaited<ReturnType<T>> = await fn(1, 2, 3);
        return ret;
    }
    async function f17_usage() {
        const x = await f17(async () => 123 as const);
        return { x };
    }
    
    // https://github.com/microsoft/TypeScript/issues/47144
    type GenericStructure<
      AcceptableKeyType extends string = string
    > = Record<AcceptableKeyType, number>;
    
    async function brokenExample<AcceptableKeyType extends string = string>(structurePromise: Promise<GenericStructure<AcceptableKeyType>>, key: AcceptableKeyType): Promise<void> {
      const structure = await structurePromise;
      structure[key] = 1;
    }
    
    // repro from #46543
    
    type SelectAndInclude = {
      select: any;
      include: any;
    };
    type HasSelect = {
      select: any;
    };
    type HasInclude = {
      include: any;
    };
    
    type CheckSelect<T, S, U> = T extends SelectAndInclude
      ? "Please either choose `select` or `include`"
      : T extends HasSelect
      ? U
      : T extends HasInclude
      ? U
      : S;
    
    declare function findMany<T extends { select?: string; include?: string }>(
      args: T
    ): CheckSelect<T, Promise<1>, Promise<2>>;
    
    async function findManyWrapper<
      T extends { select?: string; include?: string }
    >(args: T) {
      const result = await findMany(args);
      return result;
    }
    
    async function mainFindMany() {
      const shouldBeErrorText = await findManyWrapper({
        select: "foo",
        include: "bar",
      });
      const itsOne = await findManyWrapper({});
      const itsTwo1 = await findManyWrapper({ select: "foo" });
      const itsTwo2 = await findManyWrapper({ include: "bar" });
    }
    
    // repro from #41831
    
    {
      const promises = [Promise.resolve(0)] as const
    
      Promise.all(promises).then((results) => {
        const first = results[0]
        const second = results[1] // error
                               ~
!!! error TS2493: Tuple type '[number]' of length '1' has no element at index '1'.
      })
    }
    
    // repro from #40330
    
    async function test40330() {
    
        const promiseNumber = Promise.resolve(1);
        const promiseVoid = async () => {}
    
        const res = await Promise.all([
            promiseNumber,
            ...[promiseVoid()]
        ])
    }