Skip to content

Improve contextual types inferred from return types #61913

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 4 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26727,7 +26727,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
if (!inference.isFixed) {
const candidate = propagationType || source;
if (candidate === blockedStringType) {
// ReturnMapper inferences are used only for contextual types. We exclude `any` and `unknown` types
// that might otherwise swallow other useful inferences.
if (candidate === blockedStringType || candidate.flags & TypeFlags.AnyOrUnknown && priority & InferencePriority.ReturnMapper) {
return;
}
if (inference.priority === undefined || priority < inference.priority) {
Expand Down Expand Up @@ -35698,7 +35700,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// type parameters for which the inferences are being made.
const returnContext = createInferenceContext(signature.typeParameters!, signature, context.flags);
const returnSourceType = instantiateType(contextualType, outerContext && createOuterReturnMapper(outerContext));
inferTypes(returnContext.inferences, returnSourceType, inferenceTargetType);
inferTypes(returnContext.inferences, returnSourceType, inferenceTargetType, InferencePriority.ReturnMapper);
context.returnMapper = some(returnContext.inferences, hasInferenceCandidates) ? getMapperFromContext(cloneInferredPartOfContext(returnContext)) : undefined;
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7103,9 +7103,10 @@ export const enum InferencePriority {
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
ReturnMapper = 1 << 11, // Inferring for return mapper
MaxValue = 1 << 12, // Seed for inference priority tracking

PriorityImpliesCombination = ReturnType | MappedTypeConstraint | LiteralKeyof, // These priorities imply that the resulting type should be a combination of all candidates
PriorityImpliesCombination = ReturnType | ReturnMapper | 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 All @@ -7129,6 +7130,7 @@ export const enum InferenceFlags {
NoDefault = 1 << 0, // Infer silentNeverType for no inferences (otherwise anyType or unknownType)
AnyDefault = 1 << 1, // Infer anyType (in JS files) for no inferences (otherwise unknownType)
SkippedGenericFunction = 1 << 2, // A generic function was skipped during inference
NoUnknownInference = 1 << 3,
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: this stays unused

}

/**
Expand Down
5 changes: 3 additions & 2 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6894,8 +6894,9 @@ declare namespace ts {
LiteralKeyof = 256,
NoConstraints = 512,
AlwaysStrict = 1024,
MaxValue = 2048,
PriorityImpliesCombination = 416,
ReturnMapper = 2048,
MaxValue = 4096,
PriorityImpliesCombination = 2464,
Circularity = -1,
}
interface FileExtensionInfo {
Expand Down