Skip to content

Commit 55f2c0c

Browse files
authored
No synthetic Awaited for unconstrained type when not a type variable (#50100)
1 parent 394f51a commit 55f2c0c

File tree

6 files changed

+94
-2
lines changed

6 files changed

+94
-2
lines changed

src/compiler/checker.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -36710,9 +36710,11 @@ namespace ts {
3671036710
// We only need `Awaited<T>` if `T` contains possibly non-primitive types.
3671136711
if (isGenericObjectType(type)) {
3671236712
const baseConstraint = getBaseConstraintOfType(type);
36713-
// We only need `Awaited<T>` if `T` has no base constraint, or the base constraint of `T` is `any`, `unknown`, `{}`, `object`,
36713+
// We only need `Awaited<T>` if `T` is a type variable that has no base constraint, or the base constraint of `T` is `any`, `unknown`, `{}`, `object`,
3671436714
// or is promise-like.
36715-
if (!baseConstraint || (baseConstraint.flags & TypeFlags.AnyOrUnknown) || isEmptyObjectType(baseConstraint) || isThenableType(baseConstraint)) {
36715+
if (baseConstraint ?
36716+
baseConstraint.flags & TypeFlags.AnyOrUnknown || isEmptyObjectType(baseConstraint) || isThenableType(baseConstraint) :
36717+
maybeTypeOfKind(type, TypeFlags.TypeVariable)) {
3671636718
return true;
3671736719
}
3671836720
}

tests/baselines/reference/awaitedType.errors.txt

+10
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,14 @@ tests/cases/compiler/awaitedType.ts(22,12): error TS2589: Type instantiation is
177177
async function f17_usage() {
178178
const x = await f17(async () => 123 as const);
179179
return { x };
180+
}
181+
182+
// https://github.com/microsoft/TypeScript/issues/47144
183+
type GenericStructure<
184+
AcceptableKeyType extends string = string
185+
> = Record<AcceptableKeyType, number>;
186+
187+
async function brokenExample<AcceptableKeyType extends string = string>(structurePromise: Promise<GenericStructure<AcceptableKeyType>>, key: AcceptableKeyType): Promise<void> {
188+
const structure = await structurePromise;
189+
structure[key] = 1;
180190
}

tests/baselines/reference/awaitedType.js

+14
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,16 @@ async function f17<T extends (...args: any[]) => Promise<any>>(fn: T) {
169169
async function f17_usage() {
170170
const x = await f17(async () => 123 as const);
171171
return { x };
172+
}
173+
174+
// https://github.com/microsoft/TypeScript/issues/47144
175+
type GenericStructure<
176+
AcceptableKeyType extends string = string
177+
> = Record<AcceptableKeyType, number>;
178+
179+
async function brokenExample<AcceptableKeyType extends string = string>(structurePromise: Promise<GenericStructure<AcceptableKeyType>>, key: AcceptableKeyType): Promise<void> {
180+
const structure = await structurePromise;
181+
structure[key] = 1;
172182
}
173183

174184
//// [awaitedType.js]
@@ -281,3 +291,7 @@ async function f17_usage() {
281291
const x = await f17(async () => 123);
282292
return { x };
283293
}
294+
async function brokenExample(structurePromise, key) {
295+
const structure = await structurePromise;
296+
structure[key] = 1;
297+
}

tests/baselines/reference/awaitedType.symbols

+31
Original file line numberDiff line numberDiff line change
@@ -433,3 +433,34 @@ async function f17_usage() {
433433
return { x };
434434
>x : Symbol(x, Decl(awaitedType.ts, 169, 12))
435435
}
436+
437+
// https://github.com/microsoft/TypeScript/issues/47144
438+
type GenericStructure<
439+
>GenericStructure : Symbol(GenericStructure, Decl(awaitedType.ts, 170, 1))
440+
441+
AcceptableKeyType extends string = string
442+
>AcceptableKeyType : Symbol(AcceptableKeyType, Decl(awaitedType.ts, 173, 22))
443+
444+
> = Record<AcceptableKeyType, number>;
445+
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
446+
>AcceptableKeyType : Symbol(AcceptableKeyType, Decl(awaitedType.ts, 173, 22))
447+
448+
async function brokenExample<AcceptableKeyType extends string = string>(structurePromise: Promise<GenericStructure<AcceptableKeyType>>, key: AcceptableKeyType): Promise<void> {
449+
>brokenExample : Symbol(brokenExample, Decl(awaitedType.ts, 175, 38))
450+
>AcceptableKeyType : Symbol(AcceptableKeyType, Decl(awaitedType.ts, 177, 29))
451+
>structurePromise : Symbol(structurePromise, Decl(awaitedType.ts, 177, 72))
452+
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.promise.d.ts, --, --))
453+
>GenericStructure : Symbol(GenericStructure, Decl(awaitedType.ts, 170, 1))
454+
>AcceptableKeyType : Symbol(AcceptableKeyType, Decl(awaitedType.ts, 177, 29))
455+
>key : Symbol(key, Decl(awaitedType.ts, 177, 135))
456+
>AcceptableKeyType : Symbol(AcceptableKeyType, Decl(awaitedType.ts, 177, 29))
457+
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.promise.d.ts, --, --))
458+
459+
const structure = await structurePromise;
460+
>structure : Symbol(structure, Decl(awaitedType.ts, 178, 7))
461+
>structurePromise : Symbol(structurePromise, Decl(awaitedType.ts, 177, 72))
462+
463+
structure[key] = 1;
464+
>structure : Symbol(structure, Decl(awaitedType.ts, 178, 7))
465+
>key : Symbol(key, Decl(awaitedType.ts, 177, 135))
466+
}

tests/baselines/reference/awaitedType.types

+25
Original file line numberDiff line numberDiff line change
@@ -392,3 +392,28 @@ async function f17_usage() {
392392
>{ x } : { x: 123; }
393393
>x : 123
394394
}
395+
396+
// https://github.com/microsoft/TypeScript/issues/47144
397+
type GenericStructure<
398+
>GenericStructure : GenericStructure<AcceptableKeyType>
399+
400+
AcceptableKeyType extends string = string
401+
> = Record<AcceptableKeyType, number>;
402+
403+
async function brokenExample<AcceptableKeyType extends string = string>(structurePromise: Promise<GenericStructure<AcceptableKeyType>>, key: AcceptableKeyType): Promise<void> {
404+
>brokenExample : <AcceptableKeyType extends string = string>(structurePromise: Promise<GenericStructure<AcceptableKeyType>>, key: AcceptableKeyType) => Promise<void>
405+
>structurePromise : Promise<GenericStructure<AcceptableKeyType>>
406+
>key : AcceptableKeyType
407+
408+
const structure = await structurePromise;
409+
>structure : GenericStructure<AcceptableKeyType>
410+
>await structurePromise : GenericStructure<AcceptableKeyType>
411+
>structurePromise : Promise<GenericStructure<AcceptableKeyType>>
412+
413+
structure[key] = 1;
414+
>structure[key] = 1 : 1
415+
>structure[key] : GenericStructure<AcceptableKeyType>[AcceptableKeyType]
416+
>structure : GenericStructure<AcceptableKeyType>
417+
>key : AcceptableKeyType
418+
>1 : 1
419+
}

tests/cases/compiler/awaitedType.ts

+10
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,14 @@ async function f17<T extends (...args: any[]) => Promise<any>>(fn: T) {
171171
async function f17_usage() {
172172
const x = await f17(async () => 123 as const);
173173
return { x };
174+
}
175+
176+
// https://github.com/microsoft/TypeScript/issues/47144
177+
type GenericStructure<
178+
AcceptableKeyType extends string = string
179+
> = Record<AcceptableKeyType, number>;
180+
181+
async function brokenExample<AcceptableKeyType extends string = string>(structurePromise: Promise<GenericStructure<AcceptableKeyType>>, key: AcceptableKeyType): Promise<void> {
182+
const structure = await structurePromise;
183+
structure[key] = 1;
174184
}

0 commit comments

Comments
 (0)