No synthetic Awaited for unconstrained type when not a type variable (#50100)

This commit is contained in:
Ron Buckton
2022-08-01 17:19:15 -04:00
committed by GitHub
parent 394f51aeed
commit 55f2c0cb49
6 changed files with 94 additions and 2 deletions
+4 -2
View File
@@ -36710,9 +36710,11 @@ namespace ts {
// We only need `Awaited<T>` if `T` contains possibly non-primitive types.
if (isGenericObjectType(type)) {
const baseConstraint = getBaseConstraintOfType(type);
// We only need `Awaited<T>` if `T` has no base constraint, or the base constraint of `T` is `any`, `unknown`, `{}`, `object`,
// 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`,
// or is promise-like.
if (!baseConstraint || (baseConstraint.flags & TypeFlags.AnyOrUnknown) || isEmptyObjectType(baseConstraint) || isThenableType(baseConstraint)) {
if (baseConstraint ?
baseConstraint.flags & TypeFlags.AnyOrUnknown || isEmptyObjectType(baseConstraint) || isThenableType(baseConstraint) :
maybeTypeOfKind(type, TypeFlags.TypeVariable)) {
return true;
}
}
@@ -177,4 +177,14 @@ tests/cases/compiler/awaitedType.ts(22,12): error TS2589: Type instantiation is
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;
}
+14
View File
@@ -169,6 +169,16 @@ async function f17<T extends (...args: any[]) => Promise<any>>(fn: T) {
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;
}
//// [awaitedType.js]
@@ -281,3 +291,7 @@ async function f17_usage() {
const x = await f17(async () => 123);
return { x };
}
async function brokenExample(structurePromise, key) {
const structure = await structurePromise;
structure[key] = 1;
}
@@ -433,3 +433,34 @@ async function f17_usage() {
return { x };
>x : Symbol(x, Decl(awaitedType.ts, 169, 12))
}
// https://github.com/microsoft/TypeScript/issues/47144
type GenericStructure<
>GenericStructure : Symbol(GenericStructure, Decl(awaitedType.ts, 170, 1))
AcceptableKeyType extends string = string
>AcceptableKeyType : Symbol(AcceptableKeyType, Decl(awaitedType.ts, 173, 22))
> = Record<AcceptableKeyType, number>;
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
>AcceptableKeyType : Symbol(AcceptableKeyType, Decl(awaitedType.ts, 173, 22))
async function brokenExample<AcceptableKeyType extends string = string>(structurePromise: Promise<GenericStructure<AcceptableKeyType>>, key: AcceptableKeyType): Promise<void> {
>brokenExample : Symbol(brokenExample, Decl(awaitedType.ts, 175, 38))
>AcceptableKeyType : Symbol(AcceptableKeyType, Decl(awaitedType.ts, 177, 29))
>structurePromise : Symbol(structurePromise, Decl(awaitedType.ts, 177, 72))
>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, --, --))
>GenericStructure : Symbol(GenericStructure, Decl(awaitedType.ts, 170, 1))
>AcceptableKeyType : Symbol(AcceptableKeyType, Decl(awaitedType.ts, 177, 29))
>key : Symbol(key, Decl(awaitedType.ts, 177, 135))
>AcceptableKeyType : Symbol(AcceptableKeyType, Decl(awaitedType.ts, 177, 29))
>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, --, --))
const structure = await structurePromise;
>structure : Symbol(structure, Decl(awaitedType.ts, 178, 7))
>structurePromise : Symbol(structurePromise, Decl(awaitedType.ts, 177, 72))
structure[key] = 1;
>structure : Symbol(structure, Decl(awaitedType.ts, 178, 7))
>key : Symbol(key, Decl(awaitedType.ts, 177, 135))
}
@@ -392,3 +392,28 @@ async function f17_usage() {
>{ x } : { x: 123; }
>x : 123
}
// https://github.com/microsoft/TypeScript/issues/47144
type GenericStructure<
>GenericStructure : GenericStructure<AcceptableKeyType>
AcceptableKeyType extends string = string
> = Record<AcceptableKeyType, number>;
async function brokenExample<AcceptableKeyType extends string = string>(structurePromise: Promise<GenericStructure<AcceptableKeyType>>, key: AcceptableKeyType): Promise<void> {
>brokenExample : <AcceptableKeyType extends string = string>(structurePromise: Promise<GenericStructure<AcceptableKeyType>>, key: AcceptableKeyType) => Promise<void>
>structurePromise : Promise<GenericStructure<AcceptableKeyType>>
>key : AcceptableKeyType
const structure = await structurePromise;
>structure : GenericStructure<AcceptableKeyType>
>await structurePromise : GenericStructure<AcceptableKeyType>
>structurePromise : Promise<GenericStructure<AcceptableKeyType>>
structure[key] = 1;
>structure[key] = 1 : 1
>structure[key] : GenericStructure<AcceptableKeyType>[AcceptableKeyType]
>structure : GenericStructure<AcceptableKeyType>
>key : AcceptableKeyType
>1 : 1
}
+10
View File
@@ -171,4 +171,14 @@ async function f17<T extends (...args: any[]) => Promise<any>>(fn: T) {
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;
}