Skip to content

Infer rest type without using assignContextualParameterTypes #49740

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

Merged
merged 4 commits into from
Jul 6, 2022
Merged
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
18 changes: 6 additions & 12 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32472,17 +32472,6 @@ namespace ts {
}
}
}
const restType = getEffectiveRestType(context);
if (restType && restType.flags & TypeFlags.TypeParameter) {
// The contextual signature has a generic rest parameter. We first instantiate the contextual
// signature (without fixing type parameters) and assign types to contextually typed parameters.
const instantiatedContext = instantiateSignature(context, inferenceContext.nonFixingMapper);
assignContextualParameterTypes(signature, instantiatedContext);
// We then infer from a tuple type representing the parameters that correspond to the contextual
// rest parameter.
const restPos = getParameterCount(context) - 1;
inferTypes(inferenceContext.inferences, getRestTypeAtPosition(signature, restPos), restType);
}
}

function assignContextualParameterTypes(signature: Signature, context: Signature) {
Expand Down Expand Up @@ -32998,10 +32987,15 @@ namespace ts {
if (isContextSensitive(node)) {
if (contextualSignature) {
const inferenceContext = getInferenceContext(node);
let instantiatedContextualSignature: Signature | undefined;
if (checkMode && checkMode & CheckMode.Inferential) {
inferFromAnnotatedParameters(signature, contextualSignature, inferenceContext!);
const restType = getEffectiveRestType(contextualSignature);
if (restType && restType.flags & TypeFlags.TypeParameter) {
instantiatedContextualSignature = instantiateSignature(contextualSignature, inferenceContext!.nonFixingMapper);
}
}
const instantiatedContextualSignature = inferenceContext ?
instantiatedContextualSignature ||= inferenceContext ?
instantiateSignature(contextualSignature, inferenceContext.mapper) : contextualSignature;
assignContextualParameterTypes(signature, instantiatedContextualSignature);
}
Expand Down
10 changes: 10 additions & 0 deletions tests/baselines/reference/inferredRestTypeFixedOnce.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//// [inferredRestTypeFixedOnce.ts]
function wrap<Args extends unknown[]>(_: (...args: Args) => void) {}
wrap(({ cancelable } = {}) => {});


//// [inferredRestTypeFixedOnce.js]
function wrap(_) { }
wrap(function (_a) {
var _b = _a === void 0 ? {} : _a, cancelable = _b.cancelable;
});
12 changes: 12 additions & 0 deletions tests/baselines/reference/inferredRestTypeFixedOnce.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
=== tests/cases/compiler/inferredRestTypeFixedOnce.ts ===
function wrap<Args extends unknown[]>(_: (...args: Args) => void) {}
>wrap : Symbol(wrap, Decl(inferredRestTypeFixedOnce.ts, 0, 0))
>Args : Symbol(Args, Decl(inferredRestTypeFixedOnce.ts, 0, 14))
>_ : Symbol(_, Decl(inferredRestTypeFixedOnce.ts, 0, 38))
>args : Symbol(args, Decl(inferredRestTypeFixedOnce.ts, 0, 42))
>Args : Symbol(Args, Decl(inferredRestTypeFixedOnce.ts, 0, 14))

wrap(({ cancelable } = {}) => {});
>wrap : Symbol(wrap, Decl(inferredRestTypeFixedOnce.ts, 0, 0))
>cancelable : Symbol(cancelable, Decl(inferredRestTypeFixedOnce.ts, 1, 7))

13 changes: 13 additions & 0 deletions tests/baselines/reference/inferredRestTypeFixedOnce.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
=== tests/cases/compiler/inferredRestTypeFixedOnce.ts ===
function wrap<Args extends unknown[]>(_: (...args: Args) => void) {}
>wrap : <Args extends unknown[]>(_: (...args: Args) => void) => void
>_ : (...args: Args) => void
>args : Args

wrap(({ cancelable } = {}) => {});
>wrap(({ cancelable } = {}) => {}) : void
>wrap : <Args extends unknown[]>(_: (...args: Args) => void) => void
>({ cancelable } = {}) => {} : ({ cancelable }?: { cancelable: any; }) => void
>cancelable : any
>{} : {}

2 changes: 2 additions & 0 deletions tests/cases/compiler/inferredRestTypeFixedOnce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
function wrap<Args extends unknown[]>(_: (...args: Args) => void) {}
wrap(({ cancelable } = {}) => {});