Skip to content

Commit e244365

Browse files
committed
Use intersection strategy
1 parent 1583f7c commit e244365

File tree

4 files changed

+11
-12
lines changed

4 files changed

+11
-12
lines changed

src/compiler/checker.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31929,16 +31929,14 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3192931929
// return type of 'wrap'.
3193031930
if (node.kind !== SyntaxKind.Decorator) {
3193131931
const skipBindingPatterns = every(signature.typeParameters, p => !!getDefaultFromTypeParameter(p));
31932-
const contextualType = getContextualType(node, skipBindingPatterns ? ContextFlags.SkipBindingPatterns : ContextFlags.None);
31932+
let contextualType = getContextualType(node, skipBindingPatterns ? ContextFlags.SkipBindingPatterns : ContextFlags.None);
3193331933
if (contextualType) {
3193431934
const inferenceTargetType = getReturnTypeOfSignature(signature);
31935-
const targetConstraint = getConstraintOfType(inferenceTargetType);
31936-
const applicableContextualType = targetConstraint && !isTypeAssignableTo(targetConstraint, contextualType) ?
31937-
filterType(contextualType, t => isTypeAssignableTo(t, targetConstraint)) :
31938-
contextualType;
3193931935
if (couldContainTypeVariables(inferenceTargetType)) {
31936+
const targetConstraint = getConstraintOfType(inferenceTargetType);
31937+
contextualType = targetConstraint ? getIntersectionType([contextualType, targetConstraint]) : contextualType;
3194031938
const outerContext = getInferenceContext(node);
31941-
const isFromBindingPattern = !skipBindingPatterns && getContextualType(node, ContextFlags.SkipBindingPatterns) !== applicableContextualType;
31939+
const isFromBindingPattern = !skipBindingPatterns && getContextualType(node, ContextFlags.SkipBindingPatterns) !== contextualType;
3194231940
// A return type inference from a binding pattern can be used in instantiating the contextual
3194331941
// type of an argument later in inference, but cannot stand on its own as the final return type.
3194431942
// It is incorporated into `context.returnMapper` which is used in `instantiateContextualType`,
@@ -31954,7 +31952,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3195431952
// outer call expression. Effectively we just want a snapshot of whatever has been
3195531953
// inferred for any outer call expression so far.
3195631954
const outerMapper = getMapperFromContext(cloneInferenceContext(outerContext, InferenceFlags.NoDefault));
31957-
const instantiatedType = instantiateType(applicableContextualType, outerMapper);
31955+
const instantiatedType = instantiateType(contextualType, outerMapper);
3195831956
// If the contextual type is a generic function type with a single call signature, we
3195931957
// instantiate the type with its own type parameters and type arguments. This ensures that
3196031958
// the type parameters are not erased to type any during type inference such that they can
@@ -31974,7 +31972,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3197431972
// the source type uses the outer context's return mapper (which excludes inferences made from
3197531973
// outer arguments), and (b) we don't want any further inferences going into this context.
3197631974
const returnContext = createInferenceContext(signature.typeParameters!, signature, context.flags);
31977-
const returnSourceType = instantiateType(applicableContextualType, outerContext && outerContext.returnMapper);
31975+
const returnSourceType = instantiateType(contextualType, outerContext && outerContext.returnMapper);
3197831976
inferTypes(returnContext.inferences, returnSourceType, inferenceTargetType);
3197931977
context.returnMapper = some(returnContext.inferences, hasInferenceCandidates) ? getMapperFromContext(cloneInferredPartOfContext(returnContext)) : undefined;
3198031978
}

src/compiler/sys.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1602,7 +1602,8 @@ export let sys: System = (() => {
16021602
disableCPUProfiler,
16031603
cpuProfilingEnabled: () => !!activeSession || contains(process.execArgv, "--cpu-prof") || contains(process.execArgv, "--prof"),
16041604
realpath,
1605-
debugMode: !!process.env.NODE_INSPECTOR_IPC || !!process.env.VSCODE_INSPECTOR_OPTIONS || some(process.execArgv as string[], arg => /^--(inspect|debug)(-brk)?(=\d+)?$/i.test(arg)),
1605+
// debugMode: !!process.env.NODE_INSPECTOR_IPC || !!process.env.VSCODE_INSPECTOR_OPTIONS || some(process.execArgv as string[], arg => /^--(inspect|debug)(-brk)?(=\d+)?$/i.test(arg)),
1606+
debugMode: true,
16061607
tryEnableSourceMapsForHost() {
16071608
try {
16081609
(require("source-map-support") as typeof import("source-map-support")).install();

tests/baselines/reference/deepComparisons.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ function g() {
8383

8484
return f() as F<any>;
8585
>f() as F<any> : F<any>
86-
>f() : F<any>
86+
>f() : F<T>
8787
>f : <T = any>() => F<T>
8888
}
8989

tests/baselines/reference/nonnullAssertionPropegatesContextualType.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
=== tests/cases/compiler/nonnullAssertionPropegatesContextualType.ts ===
22
let rect2: SVGRectElement = document.querySelector('.svg-rectangle')!; // Error: Element
33
>rect2 : SVGRectElement
4-
>document.querySelector('.svg-rectangle')! : SVGRectElement
5-
>document.querySelector('.svg-rectangle') : SVGRectElement | null
4+
>document.querySelector('.svg-rectangle')! : SVGRectElement & Element
5+
>document.querySelector('.svg-rectangle') : (SVGRectElement & Element) | null
66
>document.querySelector : { <K extends keyof HTMLElementTagNameMap>(selectors: K): HTMLElementTagNameMap[K] | null; <K extends keyof SVGElementTagNameMap>(selectors: K): SVGElementTagNameMap[K] | null; <K extends keyof MathMLElementTagNameMap>(selectors: K): MathMLElementTagNameMap[K] | null; <K extends keyof HTMLElementDeprecatedTagNameMap>(selectors: K): HTMLElementDeprecatedTagNameMap[K] | null; <E extends Element = Element>(selectors: string): E | null; }
77
>document : Document
88
>querySelector : { <K extends keyof HTMLElementTagNameMap>(selectors: K): HTMLElementTagNameMap[K] | null; <K extends keyof SVGElementTagNameMap>(selectors: K): SVGElementTagNameMap[K] | null; <K extends keyof MathMLElementTagNameMap>(selectors: K): MathMLElementTagNameMap[K] | null; <K extends keyof HTMLElementDeprecatedTagNameMap>(selectors: K): HTMLElementDeprecatedTagNameMap[K] | null; <E extends Element = Element>(selectors: string): E | null; }

0 commit comments

Comments
 (0)