Infer rest type without using assignContextualParameterTypes (#49740)

This commit is contained in:
Jake Bailey 2022-07-06 11:27:56 -04:00 committed by GitHub
parent e75b25a444
commit 641ab8eb97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 43 additions and 12 deletions

View File

@ -32553,17 +32553,6 @@ m2: ${(this.mapper2 as unknown as DebugTypeMapper).__debugToString().split("\n")
}
}
}
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) {
@ -33079,10 +33068,15 @@ m2: ${(this.mapper2 as unknown as DebugTypeMapper).__debugToString().split("\n")
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);
}

View File

@ -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;
});

View File

@ -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))

View File

@ -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
>{} : {}

View File

@ -0,0 +1,2 @@
function wrap<Args extends unknown[]>(_: (...args: Args) => void) {}
wrap(({ cancelable } = {}) => {});