Skip to content

Commit 641ab8e

Browse files
authored
Infer rest type without using assignContextualParameterTypes (#49740)
1 parent e75b25a commit 641ab8e

File tree

5 files changed

+43
-12
lines changed

5 files changed

+43
-12
lines changed

src/compiler/checker.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32553,17 +32553,6 @@ m2: ${(this.mapper2 as unknown as DebugTypeMapper).__debugToString().split("\n")
3255332553
}
3255432554
}
3255532555
}
32556-
const restType = getEffectiveRestType(context);
32557-
if (restType && restType.flags & TypeFlags.TypeParameter) {
32558-
// The contextual signature has a generic rest parameter. We first instantiate the contextual
32559-
// signature (without fixing type parameters) and assign types to contextually typed parameters.
32560-
const instantiatedContext = instantiateSignature(context, inferenceContext.nonFixingMapper);
32561-
assignContextualParameterTypes(signature, instantiatedContext);
32562-
// We then infer from a tuple type representing the parameters that correspond to the contextual
32563-
// rest parameter.
32564-
const restPos = getParameterCount(context) - 1;
32565-
inferTypes(inferenceContext.inferences, getRestTypeAtPosition(signature, restPos), restType);
32566-
}
3256732556
}
3256832557

3256932558
function assignContextualParameterTypes(signature: Signature, context: Signature) {
@@ -33079,10 +33068,15 @@ m2: ${(this.mapper2 as unknown as DebugTypeMapper).__debugToString().split("\n")
3307933068
if (isContextSensitive(node)) {
3308033069
if (contextualSignature) {
3308133070
const inferenceContext = getInferenceContext(node);
33071+
let instantiatedContextualSignature: Signature | undefined;
3308233072
if (checkMode && checkMode & CheckMode.Inferential) {
3308333073
inferFromAnnotatedParameters(signature, contextualSignature, inferenceContext!);
33074+
const restType = getEffectiveRestType(contextualSignature);
33075+
if (restType && restType.flags & TypeFlags.TypeParameter) {
33076+
instantiatedContextualSignature = instantiateSignature(contextualSignature, inferenceContext!.nonFixingMapper);
33077+
}
3308433078
}
33085-
const instantiatedContextualSignature = inferenceContext ?
33079+
instantiatedContextualSignature ||= inferenceContext ?
3308633080
instantiateSignature(contextualSignature, inferenceContext.mapper) : contextualSignature;
3308733081
assignContextualParameterTypes(signature, instantiatedContextualSignature);
3308833082
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//// [inferredRestTypeFixedOnce.ts]
2+
function wrap<Args extends unknown[]>(_: (...args: Args) => void) {}
3+
wrap(({ cancelable } = {}) => {});
4+
5+
6+
//// [inferredRestTypeFixedOnce.js]
7+
function wrap(_) { }
8+
wrap(function (_a) {
9+
var _b = _a === void 0 ? {} : _a, cancelable = _b.cancelable;
10+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
=== tests/cases/compiler/inferredRestTypeFixedOnce.ts ===
2+
function wrap<Args extends unknown[]>(_: (...args: Args) => void) {}
3+
>wrap : Symbol(wrap, Decl(inferredRestTypeFixedOnce.ts, 0, 0))
4+
>Args : Symbol(Args, Decl(inferredRestTypeFixedOnce.ts, 0, 14))
5+
>_ : Symbol(_, Decl(inferredRestTypeFixedOnce.ts, 0, 38))
6+
>args : Symbol(args, Decl(inferredRestTypeFixedOnce.ts, 0, 42))
7+
>Args : Symbol(Args, Decl(inferredRestTypeFixedOnce.ts, 0, 14))
8+
9+
wrap(({ cancelable } = {}) => {});
10+
>wrap : Symbol(wrap, Decl(inferredRestTypeFixedOnce.ts, 0, 0))
11+
>cancelable : Symbol(cancelable, Decl(inferredRestTypeFixedOnce.ts, 1, 7))
12+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/inferredRestTypeFixedOnce.ts ===
2+
function wrap<Args extends unknown[]>(_: (...args: Args) => void) {}
3+
>wrap : <Args extends unknown[]>(_: (...args: Args) => void) => void
4+
>_ : (...args: Args) => void
5+
>args : Args
6+
7+
wrap(({ cancelable } = {}) => {});
8+
>wrap(({ cancelable } = {}) => {}) : void
9+
>wrap : <Args extends unknown[]>(_: (...args: Args) => void) => void
10+
>({ cancelable } = {}) => {} : ({ cancelable }?: { cancelable: any; }) => void
11+
>cancelable : any
12+
>{} : {}
13+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
function wrap<Args extends unknown[]>(_: (...args: Args) => void) {}
2+
wrap(({ cancelable } = {}) => {});

0 commit comments

Comments
 (0)