Skip to content

Add special inference rule for 'T | PromiseLike<T>' #60334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 52 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25992,6 +25992,42 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return isTupleType(type) && getTupleElementType(type, 0) === getIndexedAccessType(typeParameter, getNumberLiteralType(0)) && !getTypeOfPropertyOfType(type, "1" as __String);
}

function isAwaitedLikeType(type: Type) {
if (type.flags & TypeFlags.Union) {
let typeVariable: Type | undefined;
let promisedType: Type | undefined;
const types = (type as UnionType).types;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this inspect all constituents instead of eagerly returning when a match has been found?

if (types.length !== 2) {
return false;
}
for (const t of types) {
if (t.flags & TypeFlags.TypeVariable) {
if (typeVariable) {
return false;
}
typeVariable = t;
if (promisedType) {
return typeVariable === promisedType;
}
continue;
}
if (isPromiseType(t)) {
if (promisedType) {
return false;
}
[promisedType] = getTypeArguments(t as TypeReference);
if (typeVariable) {
return typeVariable === promisedType;
}
continue;
}
return false;
}
return true;
}
return false;
}

function inferTypes(inferences: InferenceInfo[], originalSource: Type, originalTarget: Type, priority = InferencePriority.None, contravariant = false) {
let bivariant = false;
let propagationType: Type;
Expand All @@ -26016,6 +26052,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
propagationType = savePropagationType;
return;
}
// If the target is either `T | Promise<T>` or `T | PromiseLike<T>`, continue inferring types with the
// `AwaitedLikeType` priority.
if (!(priority & InferencePriority.AwaitedLikeType) && isAwaitedLikeType(target)) {
priority |= InferencePriority.AwaitedLikeType;
}
if (source.aliasSymbol && source.aliasSymbol === target.aliasSymbol) {
if (source.aliasTypeArguments) {
// Source and target are types originating in the same generic type alias declaration.
Expand Down Expand Up @@ -26165,7 +26206,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
if (
getObjectFlags(source) & ObjectFlags.Reference && getObjectFlags(target) & ObjectFlags.Reference && (
(source as TypeReference).target === (target as TypeReference).target || isArrayType(source) && isArrayType(target)
(source as TypeReference).target === (target as TypeReference).target ||
isArrayType(source) && isArrayType(target) ||
isPromiseType(source) && isPromiseType(target)
) &&
!((source as TypeReference).node && (target as TypeReference).node)
) {
Expand Down Expand Up @@ -26583,7 +26626,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
function inferFromObjectTypes(source: Type, target: Type) {
if (
getObjectFlags(source) & ObjectFlags.Reference && getObjectFlags(target) & ObjectFlags.Reference && (
(source as TypeReference).target === (target as TypeReference).target || isArrayType(source) && isArrayType(target)
(source as TypeReference).target === (target as TypeReference).target ||
isArrayType(source) && isArrayType(target) ||
isPromiseType(source) && isPromiseType(target)
)
) {
// If source and target are references to the same generic type, infer from type arguments
Expand Down Expand Up @@ -42488,6 +42533,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This array can probably be cached

}

function isPromiseType(type: Type) {
return isReferenceToType(type, getGlobalPromiseType(/*reportErrors*/ false)) ||
isReferenceToType(type, getGlobalPromiseLikeType(/*reportErrors*/ false));
}

function getAwaitedTypeOfPromise(type: Type, errorNode?: Node, diagnosticMessage?: DiagnosticMessage, ...args: DiagnosticArguments): Type | undefined {
const promisedType = getPromisedTypeOfPromise(type, errorNode);
return promisedType && getAwaitedType(promisedType, errorNode, diagnosticMessage, ...args);
Expand Down
27 changes: 14 additions & 13 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7027,19 +7027,20 @@ export type TypeMapper =
export const enum InferencePriority {
None = 0,
NakedTypeVariable = 1 << 0, // Naked type variable in union or intersection type
SpeculativeTuple = 1 << 1, // Speculative tuple inference
SubstituteSource = 1 << 2, // Source of inference originated within a substitution type's substitute
HomomorphicMappedType = 1 << 3, // Reverse inference for homomorphic mapped type
PartialHomomorphicMappedType = 1 << 4, // Partial reverse inference for homomorphic mapped type
MappedTypeConstraint = 1 << 5, // Reverse inference for mapped type
ContravariantConditional = 1 << 6, // Conditional type in contravariant position
ReturnType = 1 << 7, // Inference made from return type of generic function
LiteralKeyof = 1 << 8, // Inference made from a string literal to a keyof T
NoConstraints = 1 << 9, // Don't infer from constraints of instantiable types
AlwaysStrict = 1 << 10, // Always use strict rules for contravariant inferences
MaxValue = 1 << 11, // Seed for inference priority tracking

PriorityImpliesCombination = ReturnType | MappedTypeConstraint | LiteralKeyof, // These priorities imply that the resulting type should be a combination of all candidates
AwaitedLikeType = 1 << 1, // A type that is essentially awaited
SpeculativeTuple = 1 << 2, // Speculative tuple inference
SubstituteSource = 1 << 3, // Source of inference originated within a substitution type's substitute
HomomorphicMappedType = 1 << 4, // Reverse inference for homomorphic mapped type
PartialHomomorphicMappedType = 1 << 5, // Partial reverse inference for homomorphic mapped type
MappedTypeConstraint = 1 << 6, // Reverse inference for mapped type
ContravariantConditional = 1 << 7, // Conditional type in contravariant position
ReturnType = 1 << 8, // Inference made from return type of generic function
LiteralKeyof = 1 << 9, // Inference made from a string literal to a keyof T
NoConstraints = 1 << 10, // Don't infer from constraints of instantiable types
AlwaysStrict = 1 << 11, // Always use strict rules for contravariant inferences
MaxValue = 1 << 12, // Seed for inference priority tracking

PriorityImpliesCombination = ReturnType | AwaitedLikeType | MappedTypeConstraint | LiteralKeyof, // These priorities imply that the resulting type should be a combination of all candidates
Circularity = -1, // Inference circularity (value less than all other priorities)
}

Expand Down
25 changes: 13 additions & 12 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6861,18 +6861,19 @@ declare namespace ts {
enum InferencePriority {
None = 0,
NakedTypeVariable = 1,
SpeculativeTuple = 2,
SubstituteSource = 4,
HomomorphicMappedType = 8,
PartialHomomorphicMappedType = 16,
MappedTypeConstraint = 32,
ContravariantConditional = 64,
ReturnType = 128,
LiteralKeyof = 256,
NoConstraints = 512,
AlwaysStrict = 1024,
MaxValue = 2048,
PriorityImpliesCombination = 416,
AwaitedLikeType = 2,
SpeculativeTuple = 4,
SubstituteSource = 8,
HomomorphicMappedType = 16,
PartialHomomorphicMappedType = 32,
MappedTypeConstraint = 64,
ContravariantConditional = 128,
ReturnType = 256,
LiteralKeyof = 512,
NoConstraints = 1024,
AlwaysStrict = 2048,
MaxValue = 4096,
PriorityImpliesCombination = 834,
Circularity = -1,
}
interface FileExtensionInfo {
Expand Down
28 changes: 27 additions & 1 deletion tests/baselines/reference/promiseType.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//// [promiseType.ts]
declare var p: Promise<boolean>;
declare var x: any;
declare var b: bigint;

async function A() {
const a = await p;
Expand Down Expand Up @@ -98,6 +99,7 @@ const p16 = p.catch(() => {});
const p17 = p.catch(() => {throw 1});
const p18 = p.catch(() => Promise.reject(1));
const p19 = p.catch(() => Promise.resolve(1));
const p19a = p.catch(() => b && Promise.resolve(1));

const p20 = p.then(undefined);
const p21 = p.then(null);
Expand All @@ -108,6 +110,7 @@ const p25 = p.then(() => null);
const p26 = p.then(() => {});
const p27 = p.then(() => {throw 1});
const p28 = p.then(() => Promise.resolve(1));
const p28a = p.then(() => b && Promise.resolve(1));
const p29 = p.then(() => Promise.reject(1));

const p30 = p.then(undefined, undefined);
Expand All @@ -119,6 +122,7 @@ const p35 = p.then(undefined, () => null);
const p36 = p.then(undefined, () => {});
const p37 = p.then(undefined, () => {throw 1});
const p38 = p.then(undefined, () => Promise.resolve(1));
const p38a = p.then(undefined, () => b && Promise.resolve(1));
const p39 = p.then(undefined, () => Promise.reject(1));

const p40 = p.then(null, undefined);
Expand All @@ -130,6 +134,7 @@ const p45 = p.then(null, () => null);
const p46 = p.then(null, () => {});
const p47 = p.then(null, () => {throw 1});
const p48 = p.then(null, () => Promise.resolve(1));
const p48a = p.then(null, () => b && Promise.resolve(1));
const p49 = p.then(null, () => Promise.reject(1));

const p50 = p.then(() => "1", undefined);
Expand All @@ -141,6 +146,7 @@ const p55 = p.then(() => "1", () => null);
const p56 = p.then(() => "1", () => {});
const p57 = p.then(() => "1", () => {throw 1});
const p58 = p.then(() => "1", () => Promise.resolve(1));
const p58a = p.then(() => "1", () => b && Promise.resolve(1));
const p59 = p.then(() => "1", () => Promise.reject(1));

const p60 = p.then(() => x, undefined);
Expand All @@ -152,6 +158,7 @@ const p65 = p.then(() => x, () => null);
const p66 = p.then(() => x, () => {});
const p67 = p.then(() => x, () => {throw 1});
const p68 = p.then(() => x, () => Promise.resolve(1));
const p68a = p.then(() => x, () => b && Promise.resolve(1));
const p69 = p.then(() => x, () => Promise.reject(1));

const p70 = p.then(() => undefined, undefined);
Expand All @@ -163,6 +170,7 @@ const p75 = p.then(() => undefined, () => null);
const p76 = p.then(() => undefined, () => {});
const p77 = p.then(() => undefined, () => {throw 1});
const p78 = p.then(() => undefined, () => Promise.resolve(1));
const p78a = p.then(() => undefined, () => b && Promise.resolve(1));
const p79 = p.then(() => undefined, () => Promise.reject(1));

const p80 = p.then(() => null, undefined);
Expand All @@ -174,6 +182,7 @@ const p85 = p.then(() => null, () => null);
const p86 = p.then(() => null, () => {});
const p87 = p.then(() => null, () => {throw 1});
const p88 = p.then(() => null, () => Promise.resolve(1));
const p88a = p.then(() => null, () => b && Promise.resolve(1));
const p89 = p.then(() => null, () => Promise.reject(1));

const p90 = p.then(() => {}, undefined);
Expand All @@ -185,6 +194,7 @@ const p95 = p.then(() => {}, () => null);
const p96 = p.then(() => {}, () => {});
const p97 = p.then(() => {}, () => {throw 1});
const p98 = p.then(() => {}, () => Promise.resolve(1));
const p98a = p.then(() => {}, () => b && Promise.resolve(1));
const p99 = p.then(() => {}, () => Promise.reject(1));

const pa0 = p.then(() => {throw 1}, undefined);
Expand All @@ -196,6 +206,7 @@ const pa5 = p.then(() => {throw 1}, () => null);
const pa6 = p.then(() => {throw 1}, () => {});
const pa7 = p.then(() => {throw 1}, () => {throw 1});
const pa8 = p.then(() => {throw 1}, () => Promise.resolve(1));
const pa8a = p.then(() => {throw 1}, () => b && Promise.resolve(1));
const pa9 = p.then(() => {throw 1}, () => Promise.reject(1));

const pb0 = p.then(() => Promise.resolve("1"), undefined);
Expand All @@ -207,6 +218,7 @@ const pb5 = p.then(() => Promise.resolve("1"), () => null);
const pb6 = p.then(() => Promise.resolve("1"), () => {});
const pb7 = p.then(() => Promise.resolve("1"), () => {throw 1});
const pb8 = p.then(() => Promise.resolve("1"), () => Promise.resolve(1));
const pb8a = p.then(() => Promise.resolve("1"), () => b && Promise.resolve(1));
const pb9 = p.then(() => Promise.resolve("1"), () => Promise.reject(1));

const pc0 = p.then(() => Promise.reject("1"), undefined);
Expand All @@ -218,13 +230,15 @@ const pc5 = p.then(() => Promise.reject("1"), () => null);
const pc6 = p.then(() => Promise.reject("1"), () => {});
const pc7 = p.then(() => Promise.reject("1"), () => {throw 1});
const pc8 = p.then(() => Promise.reject("1"), () => Promise.resolve(1));
const pc8a = p.then(() => Promise.reject("1"), () => b && Promise.resolve(1));
const pc9 = p.then(() => Promise.reject("1"), () => Promise.reject(1));

Promise.resolve(undefined as Promise<string> | number);
Promise.resolve(undefined as Promise<Promise<number>>);
Promise.resolve(undefined as string | Promise<Promise<number>>);
Promise.resolve(undefined as Promise<string> | Promise<Promise<number>>);
Promise.resolve(undefined as Promise<string | Promise<Promise<number>>>);
Promise.resolve(undefined as Promise<string | Promise<Promise<number>>>);


//// [promiseType.js]
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
Expand Down Expand Up @@ -337,6 +351,7 @@ const p16 = p.catch(() => { });
const p17 = p.catch(() => { throw 1; });
const p18 = p.catch(() => Promise.reject(1));
const p19 = p.catch(() => Promise.resolve(1));
const p19a = p.catch(() => b && Promise.resolve(1));
const p20 = p.then(undefined);
const p21 = p.then(null);
const p22 = p.then(() => 1);
Expand All @@ -346,6 +361,7 @@ const p25 = p.then(() => null);
const p26 = p.then(() => { });
const p27 = p.then(() => { throw 1; });
const p28 = p.then(() => Promise.resolve(1));
const p28a = p.then(() => b && Promise.resolve(1));
const p29 = p.then(() => Promise.reject(1));
const p30 = p.then(undefined, undefined);
const p31 = p.then(undefined, null);
Expand All @@ -356,6 +372,7 @@ const p35 = p.then(undefined, () => null);
const p36 = p.then(undefined, () => { });
const p37 = p.then(undefined, () => { throw 1; });
const p38 = p.then(undefined, () => Promise.resolve(1));
const p38a = p.then(undefined, () => b && Promise.resolve(1));
const p39 = p.then(undefined, () => Promise.reject(1));
const p40 = p.then(null, undefined);
const p41 = p.then(null, null);
Expand All @@ -366,6 +383,7 @@ const p45 = p.then(null, () => null);
const p46 = p.then(null, () => { });
const p47 = p.then(null, () => { throw 1; });
const p48 = p.then(null, () => Promise.resolve(1));
const p48a = p.then(null, () => b && Promise.resolve(1));
const p49 = p.then(null, () => Promise.reject(1));
const p50 = p.then(() => "1", undefined);
const p51 = p.then(() => "1", null);
Expand All @@ -376,6 +394,7 @@ const p55 = p.then(() => "1", () => null);
const p56 = p.then(() => "1", () => { });
const p57 = p.then(() => "1", () => { throw 1; });
const p58 = p.then(() => "1", () => Promise.resolve(1));
const p58a = p.then(() => "1", () => b && Promise.resolve(1));
const p59 = p.then(() => "1", () => Promise.reject(1));
const p60 = p.then(() => x, undefined);
const p61 = p.then(() => x, null);
Expand All @@ -386,6 +405,7 @@ const p65 = p.then(() => x, () => null);
const p66 = p.then(() => x, () => { });
const p67 = p.then(() => x, () => { throw 1; });
const p68 = p.then(() => x, () => Promise.resolve(1));
const p68a = p.then(() => x, () => b && Promise.resolve(1));
const p69 = p.then(() => x, () => Promise.reject(1));
const p70 = p.then(() => undefined, undefined);
const p71 = p.then(() => undefined, null);
Expand All @@ -396,6 +416,7 @@ const p75 = p.then(() => undefined, () => null);
const p76 = p.then(() => undefined, () => { });
const p77 = p.then(() => undefined, () => { throw 1; });
const p78 = p.then(() => undefined, () => Promise.resolve(1));
const p78a = p.then(() => undefined, () => b && Promise.resolve(1));
const p79 = p.then(() => undefined, () => Promise.reject(1));
const p80 = p.then(() => null, undefined);
const p81 = p.then(() => null, null);
Expand All @@ -406,6 +427,7 @@ const p85 = p.then(() => null, () => null);
const p86 = p.then(() => null, () => { });
const p87 = p.then(() => null, () => { throw 1; });
const p88 = p.then(() => null, () => Promise.resolve(1));
const p88a = p.then(() => null, () => b && Promise.resolve(1));
const p89 = p.then(() => null, () => Promise.reject(1));
const p90 = p.then(() => { }, undefined);
const p91 = p.then(() => { }, null);
Expand All @@ -416,6 +438,7 @@ const p95 = p.then(() => { }, () => null);
const p96 = p.then(() => { }, () => { });
const p97 = p.then(() => { }, () => { throw 1; });
const p98 = p.then(() => { }, () => Promise.resolve(1));
const p98a = p.then(() => { }, () => b && Promise.resolve(1));
const p99 = p.then(() => { }, () => Promise.reject(1));
const pa0 = p.then(() => { throw 1; }, undefined);
const pa1 = p.then(() => { throw 1; }, null);
Expand All @@ -426,6 +449,7 @@ const pa5 = p.then(() => { throw 1; }, () => null);
const pa6 = p.then(() => { throw 1; }, () => { });
const pa7 = p.then(() => { throw 1; }, () => { throw 1; });
const pa8 = p.then(() => { throw 1; }, () => Promise.resolve(1));
const pa8a = p.then(() => { throw 1; }, () => b && Promise.resolve(1));
const pa9 = p.then(() => { throw 1; }, () => Promise.reject(1));
const pb0 = p.then(() => Promise.resolve("1"), undefined);
const pb1 = p.then(() => Promise.resolve("1"), null);
Expand All @@ -436,6 +460,7 @@ const pb5 = p.then(() => Promise.resolve("1"), () => null);
const pb6 = p.then(() => Promise.resolve("1"), () => { });
const pb7 = p.then(() => Promise.resolve("1"), () => { throw 1; });
const pb8 = p.then(() => Promise.resolve("1"), () => Promise.resolve(1));
const pb8a = p.then(() => Promise.resolve("1"), () => b && Promise.resolve(1));
const pb9 = p.then(() => Promise.resolve("1"), () => Promise.reject(1));
const pc0 = p.then(() => Promise.reject("1"), undefined);
const pc1 = p.then(() => Promise.reject("1"), null);
Expand All @@ -446,6 +471,7 @@ const pc5 = p.then(() => Promise.reject("1"), () => null);
const pc6 = p.then(() => Promise.reject("1"), () => { });
const pc7 = p.then(() => Promise.reject("1"), () => { throw 1; });
const pc8 = p.then(() => Promise.reject("1"), () => Promise.resolve(1));
const pc8a = p.then(() => Promise.reject("1"), () => b && Promise.resolve(1));
const pc9 = p.then(() => Promise.reject("1"), () => Promise.reject(1));
Promise.resolve(undefined);
Promise.resolve(undefined);
Expand Down
Loading