From cc49efddad545665ddfbabc83f6bb2488daa13f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Sat, 18 Nov 2023 19:47:37 +0100 Subject: [PATCH 1/3] Infer from annotated parameters of context sensitive functions in the first inference pass --- src/compiler/checker.ts | 24 +++++-- ...sitiveAnnotatedParametersInference.symbols | 70 ++++++++++++++++++ ...ensitiveAnnotatedParametersInference.types | 71 +++++++++++++++++++ ...xtSensitiveAnnotatedParametersInference.ts | 20 ++++++ 4 files changed, 178 insertions(+), 7 deletions(-) create mode 100644 tests/baselines/reference/contextSensitiveAnnotatedParametersInference.symbols create mode 100644 tests/baselines/reference/contextSensitiveAnnotatedParametersInference.types create mode 100644 tests/cases/compiler/contextSensitiveAnnotatedParametersInference.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index f27a6058006e5..58ae988db982c 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -36023,14 +36023,18 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { return signature.parameters.length > 0 ? getTypeAtPosition(signature, 0) : fallbackType; } - function inferFromAnnotatedParameters(signature: Signature, context: Signature, inferenceContext: InferenceContext) { - const len = signature.parameters.length - (signatureHasRestParameter(signature) ? 1 : 0); - for (let i = 0; i < len; i++) { - const declaration = signature.parameters[i].valueDeclaration as ParameterDeclaration; + function inferFromAnnotatedParameters(node: FunctionExpression | ArrowFunction | MethodDeclaration, contextualSignature: Signature, inferenceContext: InferenceContext) { + if (!node.parameters.length) { + return; + } + const len = node.parameters.length - (last(node.parameters).dotDotDotToken ? 1 : 0); + const thisParameterOffset = first(node.parameters).symbol.escapedName === InternalSymbolName.This ? 1 : 0; + for (let i = thisParameterOffset; i < len; i++) { + const declaration = node.parameters[i]; const typeNode = getEffectiveTypeAnnotationNode(declaration); if (typeNode) { const source = addOptionality(getTypeFromTypeNode(typeNode), /*isProperty*/ false, isOptionalDeclaration(declaration)); - const target = getTypeAtPosition(context, i); + const target = getTypeAtPosition(contextualSignature, i - thisParameterOffset); inferTypes(inferenceContext.inferences, source, target); } } @@ -36947,6 +36951,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { return links.contextFreeType = returnOnlyType; } } + if (checkMode & CheckMode.Inferential) { + const contextualSignature = getContextualSignature(node); + if (contextualSignature) { + inferFromAnnotatedParameters(node, contextualSignature, getInferenceContext(node)!); + } + } return anyFunctionType; } @@ -36980,7 +36990,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { const inferenceContext = getInferenceContext(node); let instantiatedContextualSignature: Signature | undefined; if (checkMode && checkMode & CheckMode.Inferential) { - inferFromAnnotatedParameters(signature, contextualSignature, inferenceContext!); + inferFromAnnotatedParameters(node, contextualSignature, inferenceContext!); const restType = getEffectiveRestType(contextualSignature); if (restType && restType.flags & TypeFlags.TypeParameter) { instantiatedContextualSignature = instantiateSignature(contextualSignature, inferenceContext!.nonFixingMapper); @@ -36998,7 +37008,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { else if (contextualSignature && !node.typeParameters && contextualSignature.parameters.length > node.parameters.length) { const inferenceContext = getInferenceContext(node); if (checkMode && checkMode & CheckMode.Inferential) { - inferFromAnnotatedParameters(signature, contextualSignature, inferenceContext!); + inferFromAnnotatedParameters(node, contextualSignature, inferenceContext!); } } if (contextualSignature && !getReturnTypeFromAnnotation(node) && !signature.resolvedReturnType) { diff --git a/tests/baselines/reference/contextSensitiveAnnotatedParametersInference.symbols b/tests/baselines/reference/contextSensitiveAnnotatedParametersInference.symbols new file mode 100644 index 0000000000000..3870250d9f8f7 --- /dev/null +++ b/tests/baselines/reference/contextSensitiveAnnotatedParametersInference.symbols @@ -0,0 +1,70 @@ +//// [tests/cases/compiler/contextSensitiveAnnotatedParametersInference.ts] //// + +=== contextSensitiveAnnotatedParametersInference.ts === +declare function test(obj: { +>test : Symbol(test, Decl(contextSensitiveAnnotatedParametersInference.ts, 0, 0)) +>T : Symbol(T, Decl(contextSensitiveAnnotatedParametersInference.ts, 0, 22)) +>A : Symbol(A, Decl(contextSensitiveAnnotatedParametersInference.ts, 0, 24)) +>B : Symbol(B, Decl(contextSensitiveAnnotatedParametersInference.ts, 0, 27)) +>obj : Symbol(obj, Decl(contextSensitiveAnnotatedParametersInference.ts, 0, 31)) + + ctx: T; +>ctx : Symbol(ctx, Decl(contextSensitiveAnnotatedParametersInference.ts, 0, 37)) +>T : Symbol(T, Decl(contextSensitiveAnnotatedParametersInference.ts, 0, 22)) + + a: (a: A, ctx: T) => void; +>a : Symbol(a, Decl(contextSensitiveAnnotatedParametersInference.ts, 1, 9)) +>a : Symbol(a, Decl(contextSensitiveAnnotatedParametersInference.ts, 2, 6)) +>A : Symbol(A, Decl(contextSensitiveAnnotatedParametersInference.ts, 0, 24)) +>ctx : Symbol(ctx, Decl(contextSensitiveAnnotatedParametersInference.ts, 2, 11)) +>T : Symbol(T, Decl(contextSensitiveAnnotatedParametersInference.ts, 0, 22)) + + b: (b: B, ctx: T, a: A) => void; +>b : Symbol(b, Decl(contextSensitiveAnnotatedParametersInference.ts, 2, 28)) +>b : Symbol(b, Decl(contextSensitiveAnnotatedParametersInference.ts, 3, 6)) +>B : Symbol(B, Decl(contextSensitiveAnnotatedParametersInference.ts, 0, 27)) +>ctx : Symbol(ctx, Decl(contextSensitiveAnnotatedParametersInference.ts, 3, 11)) +>T : Symbol(T, Decl(contextSensitiveAnnotatedParametersInference.ts, 0, 22)) +>a : Symbol(a, Decl(contextSensitiveAnnotatedParametersInference.ts, 3, 19)) +>A : Symbol(A, Decl(contextSensitiveAnnotatedParametersInference.ts, 0, 24)) + +}): void; + +test({ +>test : Symbol(test, Decl(contextSensitiveAnnotatedParametersInference.ts, 0, 0)) + + ctx: 'foo', +>ctx : Symbol(ctx, Decl(contextSensitiveAnnotatedParametersInference.ts, 6, 6)) + + a: (a: string, ctx) => {}, +>a : Symbol(a, Decl(contextSensitiveAnnotatedParametersInference.ts, 7, 13)) +>a : Symbol(a, Decl(contextSensitiveAnnotatedParametersInference.ts, 8, 6)) +>ctx : Symbol(ctx, Decl(contextSensitiveAnnotatedParametersInference.ts, 8, 16)) + + b: (b: string, ctx, a) => {}, +>b : Symbol(b, Decl(contextSensitiveAnnotatedParametersInference.ts, 8, 28)) +>b : Symbol(b, Decl(contextSensitiveAnnotatedParametersInference.ts, 9, 6)) +>ctx : Symbol(ctx, Decl(contextSensitiveAnnotatedParametersInference.ts, 9, 16)) +>a : Symbol(a, Decl(contextSensitiveAnnotatedParametersInference.ts, 9, 21)) + +}); + +test({ +>test : Symbol(test, Decl(contextSensitiveAnnotatedParametersInference.ts, 0, 0)) + + ctx: 'foo', +>ctx : Symbol(ctx, Decl(contextSensitiveAnnotatedParametersInference.ts, 12, 6)) + + b: (b: string, ctx, a) => {}, +>b : Symbol(b, Decl(contextSensitiveAnnotatedParametersInference.ts, 13, 13)) +>b : Symbol(b, Decl(contextSensitiveAnnotatedParametersInference.ts, 14, 6)) +>ctx : Symbol(ctx, Decl(contextSensitiveAnnotatedParametersInference.ts, 14, 16)) +>a : Symbol(a, Decl(contextSensitiveAnnotatedParametersInference.ts, 14, 21)) + + a: (a: string, ctx) => {}, +>a : Symbol(a, Decl(contextSensitiveAnnotatedParametersInference.ts, 14, 31)) +>a : Symbol(a, Decl(contextSensitiveAnnotatedParametersInference.ts, 15, 6)) +>ctx : Symbol(ctx, Decl(contextSensitiveAnnotatedParametersInference.ts, 15, 16)) + +}); + diff --git a/tests/baselines/reference/contextSensitiveAnnotatedParametersInference.types b/tests/baselines/reference/contextSensitiveAnnotatedParametersInference.types new file mode 100644 index 0000000000000..eb85734f9dba1 --- /dev/null +++ b/tests/baselines/reference/contextSensitiveAnnotatedParametersInference.types @@ -0,0 +1,71 @@ +//// [tests/cases/compiler/contextSensitiveAnnotatedParametersInference.ts] //// + +=== contextSensitiveAnnotatedParametersInference.ts === +declare function test(obj: { +>test : (obj: { ctx: T; a: (a: A, ctx: T) => void; b: (b: B, ctx: T, a: A) => void; }) => void +>obj : { ctx: T; a: (a: A, ctx: T) => void; b: (b: B, ctx: T, a: A) => void; } + + ctx: T; +>ctx : T + + a: (a: A, ctx: T) => void; +>a : (a: A, ctx: T) => void +>a : A +>ctx : T + + b: (b: B, ctx: T, a: A) => void; +>b : (b: B, ctx: T, a: A) => void +>b : B +>ctx : T +>a : A + +}): void; + +test({ +>test({ ctx: 'foo', a: (a: string, ctx) => {}, b: (b: string, ctx, a) => {},}) : void +>test : (obj: { ctx: T; a: (a: A, ctx: T) => void; b: (b: B, ctx: T, a: A) => void; }) => void +>{ ctx: 'foo', a: (a: string, ctx) => {}, b: (b: string, ctx, a) => {},} : { ctx: string; a: (a: string, ctx: string) => void; b: (b: string, ctx: string, a: string) => void; } + + ctx: 'foo', +>ctx : string +>'foo' : "foo" + + a: (a: string, ctx) => {}, +>a : (a: string, ctx: string) => void +>(a: string, ctx) => {} : (a: string, ctx: string) => void +>a : string +>ctx : string + + b: (b: string, ctx, a) => {}, +>b : (b: string, ctx: string, a: string) => void +>(b: string, ctx, a) => {} : (b: string, ctx: string, a: string) => void +>b : string +>ctx : string +>a : string + +}); + +test({ +>test({ ctx: 'foo', b: (b: string, ctx, a) => {}, a: (a: string, ctx) => {},}) : void +>test : (obj: { ctx: T; a: (a: A, ctx: T) => void; b: (b: B, ctx: T, a: A) => void; }) => void +>{ ctx: 'foo', b: (b: string, ctx, a) => {}, a: (a: string, ctx) => {},} : { ctx: string; b: (b: string, ctx: string, a: string) => void; a: (a: string, ctx: string) => void; } + + ctx: 'foo', +>ctx : string +>'foo' : "foo" + + b: (b: string, ctx, a) => {}, +>b : (b: string, ctx: string, a: string) => void +>(b: string, ctx, a) => {} : (b: string, ctx: string, a: string) => void +>b : string +>ctx : string +>a : string + + a: (a: string, ctx) => {}, +>a : (a: string, ctx: string) => void +>(a: string, ctx) => {} : (a: string, ctx: string) => void +>a : string +>ctx : string + +}); + diff --git a/tests/cases/compiler/contextSensitiveAnnotatedParametersInference.ts b/tests/cases/compiler/contextSensitiveAnnotatedParametersInference.ts new file mode 100644 index 0000000000000..ea27840069acc --- /dev/null +++ b/tests/cases/compiler/contextSensitiveAnnotatedParametersInference.ts @@ -0,0 +1,20 @@ +// @strict: true +// @noEmit: true + +declare function test(obj: { + ctx: T; + a: (a: A, ctx: T) => void; + b: (b: B, ctx: T, a: A) => void; +}): void; + +test({ + ctx: 'foo', + a: (a: string, ctx) => {}, + b: (b: string, ctx, a) => {}, +}); + +test({ + ctx: 'foo', + b: (b: string, ctx, a) => {}, + a: (a: string, ctx) => {}, +}); From ac20ac6cccc3e29b4f673924ab1f52e39a8bae7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Tue, 24 Sep 2024 18:53:29 +0200 Subject: [PATCH 2/3] add extra tests --- ...tiveAnnotatedParametersInference1.symbols} | 78 +++++------ ...sitiveAnnotatedParametersInference1.types} | 4 +- ...itiveAnnotatedParametersInference2.symbols | 91 +++++++++++++ ...nsitiveAnnotatedParametersInference2.types | 127 ++++++++++++++++++ ...SensitiveAnnotatedParametersInference1.ts} | 0 ...tSensitiveAnnotatedParametersInference2.ts | 36 +++++ 6 files changed, 295 insertions(+), 41 deletions(-) rename tests/baselines/reference/{contextSensitiveAnnotatedParametersInference.symbols => contextSensitiveAnnotatedParametersInference1.symbols} (79%) rename tests/baselines/reference/{contextSensitiveAnnotatedParametersInference.types => contextSensitiveAnnotatedParametersInference1.types} (95%) create mode 100644 tests/baselines/reference/contextSensitiveAnnotatedParametersInference2.symbols create mode 100644 tests/baselines/reference/contextSensitiveAnnotatedParametersInference2.types rename tests/cases/compiler/{contextSensitiveAnnotatedParametersInference.ts => contextSensitiveAnnotatedParametersInference1.ts} (100%) create mode 100644 tests/cases/compiler/contextSensitiveAnnotatedParametersInference2.ts diff --git a/tests/baselines/reference/contextSensitiveAnnotatedParametersInference.symbols b/tests/baselines/reference/contextSensitiveAnnotatedParametersInference1.symbols similarity index 79% rename from tests/baselines/reference/contextSensitiveAnnotatedParametersInference.symbols rename to tests/baselines/reference/contextSensitiveAnnotatedParametersInference1.symbols index 3870250d9f8f7..7c47e93f14e12 100644 --- a/tests/baselines/reference/contextSensitiveAnnotatedParametersInference.symbols +++ b/tests/baselines/reference/contextSensitiveAnnotatedParametersInference1.symbols @@ -1,70 +1,70 @@ -//// [tests/cases/compiler/contextSensitiveAnnotatedParametersInference.ts] //// +//// [tests/cases/compiler/contextSensitiveAnnotatedParametersInference1.ts] //// -=== contextSensitiveAnnotatedParametersInference.ts === +=== contextSensitiveAnnotatedParametersInference1.ts === declare function test(obj: { ->test : Symbol(test, Decl(contextSensitiveAnnotatedParametersInference.ts, 0, 0)) ->T : Symbol(T, Decl(contextSensitiveAnnotatedParametersInference.ts, 0, 22)) ->A : Symbol(A, Decl(contextSensitiveAnnotatedParametersInference.ts, 0, 24)) ->B : Symbol(B, Decl(contextSensitiveAnnotatedParametersInference.ts, 0, 27)) ->obj : Symbol(obj, Decl(contextSensitiveAnnotatedParametersInference.ts, 0, 31)) +>test : Symbol(test, Decl(contextSensitiveAnnotatedParametersInference1.ts, 0, 0)) +>T : Symbol(T, Decl(contextSensitiveAnnotatedParametersInference1.ts, 0, 22)) +>A : Symbol(A, Decl(contextSensitiveAnnotatedParametersInference1.ts, 0, 24)) +>B : Symbol(B, Decl(contextSensitiveAnnotatedParametersInference1.ts, 0, 27)) +>obj : Symbol(obj, Decl(contextSensitiveAnnotatedParametersInference1.ts, 0, 31)) ctx: T; ->ctx : Symbol(ctx, Decl(contextSensitiveAnnotatedParametersInference.ts, 0, 37)) ->T : Symbol(T, Decl(contextSensitiveAnnotatedParametersInference.ts, 0, 22)) +>ctx : Symbol(ctx, Decl(contextSensitiveAnnotatedParametersInference1.ts, 0, 37)) +>T : Symbol(T, Decl(contextSensitiveAnnotatedParametersInference1.ts, 0, 22)) a: (a: A, ctx: T) => void; ->a : Symbol(a, Decl(contextSensitiveAnnotatedParametersInference.ts, 1, 9)) ->a : Symbol(a, Decl(contextSensitiveAnnotatedParametersInference.ts, 2, 6)) ->A : Symbol(A, Decl(contextSensitiveAnnotatedParametersInference.ts, 0, 24)) ->ctx : Symbol(ctx, Decl(contextSensitiveAnnotatedParametersInference.ts, 2, 11)) ->T : Symbol(T, Decl(contextSensitiveAnnotatedParametersInference.ts, 0, 22)) +>a : Symbol(a, Decl(contextSensitiveAnnotatedParametersInference1.ts, 1, 9)) +>a : Symbol(a, Decl(contextSensitiveAnnotatedParametersInference1.ts, 2, 6)) +>A : Symbol(A, Decl(contextSensitiveAnnotatedParametersInference1.ts, 0, 24)) +>ctx : Symbol(ctx, Decl(contextSensitiveAnnotatedParametersInference1.ts, 2, 11)) +>T : Symbol(T, Decl(contextSensitiveAnnotatedParametersInference1.ts, 0, 22)) b: (b: B, ctx: T, a: A) => void; ->b : Symbol(b, Decl(contextSensitiveAnnotatedParametersInference.ts, 2, 28)) ->b : Symbol(b, Decl(contextSensitiveAnnotatedParametersInference.ts, 3, 6)) ->B : Symbol(B, Decl(contextSensitiveAnnotatedParametersInference.ts, 0, 27)) ->ctx : Symbol(ctx, Decl(contextSensitiveAnnotatedParametersInference.ts, 3, 11)) ->T : Symbol(T, Decl(contextSensitiveAnnotatedParametersInference.ts, 0, 22)) ->a : Symbol(a, Decl(contextSensitiveAnnotatedParametersInference.ts, 3, 19)) ->A : Symbol(A, Decl(contextSensitiveAnnotatedParametersInference.ts, 0, 24)) +>b : Symbol(b, Decl(contextSensitiveAnnotatedParametersInference1.ts, 2, 28)) +>b : Symbol(b, Decl(contextSensitiveAnnotatedParametersInference1.ts, 3, 6)) +>B : Symbol(B, Decl(contextSensitiveAnnotatedParametersInference1.ts, 0, 27)) +>ctx : Symbol(ctx, Decl(contextSensitiveAnnotatedParametersInference1.ts, 3, 11)) +>T : Symbol(T, Decl(contextSensitiveAnnotatedParametersInference1.ts, 0, 22)) +>a : Symbol(a, Decl(contextSensitiveAnnotatedParametersInference1.ts, 3, 19)) +>A : Symbol(A, Decl(contextSensitiveAnnotatedParametersInference1.ts, 0, 24)) }): void; test({ ->test : Symbol(test, Decl(contextSensitiveAnnotatedParametersInference.ts, 0, 0)) +>test : Symbol(test, Decl(contextSensitiveAnnotatedParametersInference1.ts, 0, 0)) ctx: 'foo', ->ctx : Symbol(ctx, Decl(contextSensitiveAnnotatedParametersInference.ts, 6, 6)) +>ctx : Symbol(ctx, Decl(contextSensitiveAnnotatedParametersInference1.ts, 6, 6)) a: (a: string, ctx) => {}, ->a : Symbol(a, Decl(contextSensitiveAnnotatedParametersInference.ts, 7, 13)) ->a : Symbol(a, Decl(contextSensitiveAnnotatedParametersInference.ts, 8, 6)) ->ctx : Symbol(ctx, Decl(contextSensitiveAnnotatedParametersInference.ts, 8, 16)) +>a : Symbol(a, Decl(contextSensitiveAnnotatedParametersInference1.ts, 7, 13)) +>a : Symbol(a, Decl(contextSensitiveAnnotatedParametersInference1.ts, 8, 6)) +>ctx : Symbol(ctx, Decl(contextSensitiveAnnotatedParametersInference1.ts, 8, 16)) b: (b: string, ctx, a) => {}, ->b : Symbol(b, Decl(contextSensitiveAnnotatedParametersInference.ts, 8, 28)) ->b : Symbol(b, Decl(contextSensitiveAnnotatedParametersInference.ts, 9, 6)) ->ctx : Symbol(ctx, Decl(contextSensitiveAnnotatedParametersInference.ts, 9, 16)) ->a : Symbol(a, Decl(contextSensitiveAnnotatedParametersInference.ts, 9, 21)) +>b : Symbol(b, Decl(contextSensitiveAnnotatedParametersInference1.ts, 8, 28)) +>b : Symbol(b, Decl(contextSensitiveAnnotatedParametersInference1.ts, 9, 6)) +>ctx : Symbol(ctx, Decl(contextSensitiveAnnotatedParametersInference1.ts, 9, 16)) +>a : Symbol(a, Decl(contextSensitiveAnnotatedParametersInference1.ts, 9, 21)) }); test({ ->test : Symbol(test, Decl(contextSensitiveAnnotatedParametersInference.ts, 0, 0)) +>test : Symbol(test, Decl(contextSensitiveAnnotatedParametersInference1.ts, 0, 0)) ctx: 'foo', ->ctx : Symbol(ctx, Decl(contextSensitiveAnnotatedParametersInference.ts, 12, 6)) +>ctx : Symbol(ctx, Decl(contextSensitiveAnnotatedParametersInference1.ts, 12, 6)) b: (b: string, ctx, a) => {}, ->b : Symbol(b, Decl(contextSensitiveAnnotatedParametersInference.ts, 13, 13)) ->b : Symbol(b, Decl(contextSensitiveAnnotatedParametersInference.ts, 14, 6)) ->ctx : Symbol(ctx, Decl(contextSensitiveAnnotatedParametersInference.ts, 14, 16)) ->a : Symbol(a, Decl(contextSensitiveAnnotatedParametersInference.ts, 14, 21)) +>b : Symbol(b, Decl(contextSensitiveAnnotatedParametersInference1.ts, 13, 13)) +>b : Symbol(b, Decl(contextSensitiveAnnotatedParametersInference1.ts, 14, 6)) +>ctx : Symbol(ctx, Decl(contextSensitiveAnnotatedParametersInference1.ts, 14, 16)) +>a : Symbol(a, Decl(contextSensitiveAnnotatedParametersInference1.ts, 14, 21)) a: (a: string, ctx) => {}, ->a : Symbol(a, Decl(contextSensitiveAnnotatedParametersInference.ts, 14, 31)) ->a : Symbol(a, Decl(contextSensitiveAnnotatedParametersInference.ts, 15, 6)) ->ctx : Symbol(ctx, Decl(contextSensitiveAnnotatedParametersInference.ts, 15, 16)) +>a : Symbol(a, Decl(contextSensitiveAnnotatedParametersInference1.ts, 14, 31)) +>a : Symbol(a, Decl(contextSensitiveAnnotatedParametersInference1.ts, 15, 6)) +>ctx : Symbol(ctx, Decl(contextSensitiveAnnotatedParametersInference1.ts, 15, 16)) }); diff --git a/tests/baselines/reference/contextSensitiveAnnotatedParametersInference.types b/tests/baselines/reference/contextSensitiveAnnotatedParametersInference1.types similarity index 95% rename from tests/baselines/reference/contextSensitiveAnnotatedParametersInference.types rename to tests/baselines/reference/contextSensitiveAnnotatedParametersInference1.types index 5b017cf048a5a..2709d3f579023 100644 --- a/tests/baselines/reference/contextSensitiveAnnotatedParametersInference.types +++ b/tests/baselines/reference/contextSensitiveAnnotatedParametersInference1.types @@ -1,6 +1,6 @@ -//// [tests/cases/compiler/contextSensitiveAnnotatedParametersInference.ts] //// +//// [tests/cases/compiler/contextSensitiveAnnotatedParametersInference1.ts] //// -=== contextSensitiveAnnotatedParametersInference.ts === +=== contextSensitiveAnnotatedParametersInference1.ts === declare function test(obj: { >test : (obj: { ctx: T; a: (a: A, ctx: T) => void; b: (b: B, ctx: T, a: A) => void; }) => void > : ^ ^^ ^^ ^^ ^^ ^^^^^ diff --git a/tests/baselines/reference/contextSensitiveAnnotatedParametersInference2.symbols b/tests/baselines/reference/contextSensitiveAnnotatedParametersInference2.symbols new file mode 100644 index 0000000000000..89129c6fb6830 --- /dev/null +++ b/tests/baselines/reference/contextSensitiveAnnotatedParametersInference2.symbols @@ -0,0 +1,91 @@ +//// [tests/cases/compiler/contextSensitiveAnnotatedParametersInference2.ts] //// + +=== contextSensitiveAnnotatedParametersInference2.ts === +// https://github.com/microsoft/TypeScript/issues/60047 + +type Map = { +>Map : Symbol(Map, Decl(contextSensitiveAnnotatedParametersInference2.ts, 0, 0)) +>T : Symbol(T, Decl(contextSensitiveAnnotatedParametersInference2.ts, 2, 9)) + + [P in keyof T]: T[P] extends boolean ? "boolean" : "other"; +>P : Symbol(P, Decl(contextSensitiveAnnotatedParametersInference2.ts, 3, 3)) +>T : Symbol(T, Decl(contextSensitiveAnnotatedParametersInference2.ts, 2, 9)) +>T : Symbol(T, Decl(contextSensitiveAnnotatedParametersInference2.ts, 2, 9)) +>P : Symbol(P, Decl(contextSensitiveAnnotatedParametersInference2.ts, 3, 3)) + +}; + +export function buildCommand>(builderArgs: { +>buildCommand : Symbol(buildCommand, Decl(contextSensitiveAnnotatedParametersInference2.ts, 4, 2)) +>F : Symbol(F, Decl(contextSensitiveAnnotatedParametersInference2.ts, 6, 29)) +>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --)) +>builderArgs : Symbol(builderArgs, Decl(contextSensitiveAnnotatedParametersInference2.ts, 6, 64)) + + func: (p: F) => void; +>func : Symbol(func, Decl(contextSensitiveAnnotatedParametersInference2.ts, 6, 78)) +>p : Symbol(p, Decl(contextSensitiveAnnotatedParametersInference2.ts, 7, 9)) +>F : Symbol(F, Decl(contextSensitiveAnnotatedParametersInference2.ts, 6, 29)) + + params: Map>; +>params : Symbol(params, Decl(contextSensitiveAnnotatedParametersInference2.ts, 7, 23)) +>Map : Symbol(Map, Decl(contextSensitiveAnnotatedParametersInference2.ts, 0, 0)) +>NoInfer : Symbol(NoInfer, Decl(lib.es5.d.ts, --, --)) +>F : Symbol(F, Decl(contextSensitiveAnnotatedParametersInference2.ts, 6, 29)) + +}) {} + +type Foo = { foo: boolean }; +>Foo : Symbol(Foo, Decl(contextSensitiveAnnotatedParametersInference2.ts, 9, 5)) +>foo : Symbol(foo, Decl(contextSensitiveAnnotatedParametersInference2.ts, 11, 12)) + +buildCommand({ +>buildCommand : Symbol(buildCommand, Decl(contextSensitiveAnnotatedParametersInference2.ts, 4, 2)) + + func: function (p: Foo) {}, +>func : Symbol(func, Decl(contextSensitiveAnnotatedParametersInference2.ts, 13, 14)) +>p : Symbol(p, Decl(contextSensitiveAnnotatedParametersInference2.ts, 14, 18)) +>Foo : Symbol(Foo, Decl(contextSensitiveAnnotatedParametersInference2.ts, 9, 5)) + + params: { +>params : Symbol(params, Decl(contextSensitiveAnnotatedParametersInference2.ts, 14, 29)) + + foo: "boolean", +>foo : Symbol(foo, Decl(contextSensitiveAnnotatedParametersInference2.ts, 15, 11)) + + }, +}); + +buildCommand({ +>buildCommand : Symbol(buildCommand, Decl(contextSensitiveAnnotatedParametersInference2.ts, 4, 2)) + + func(p: Foo) {}, +>func : Symbol(func, Decl(contextSensitiveAnnotatedParametersInference2.ts, 20, 14)) +>p : Symbol(p, Decl(contextSensitiveAnnotatedParametersInference2.ts, 21, 7)) +>Foo : Symbol(Foo, Decl(contextSensitiveAnnotatedParametersInference2.ts, 9, 5)) + + params: { +>params : Symbol(params, Decl(contextSensitiveAnnotatedParametersInference2.ts, 21, 18)) + + foo: "boolean", +>foo : Symbol(foo, Decl(contextSensitiveAnnotatedParametersInference2.ts, 22, 11)) + + }, +}); + +buildCommand({ +>buildCommand : Symbol(buildCommand, Decl(contextSensitiveAnnotatedParametersInference2.ts, 4, 2)) + + func: (p: Foo) => {}, +>func : Symbol(func, Decl(contextSensitiveAnnotatedParametersInference2.ts, 27, 14)) +>p : Symbol(p, Decl(contextSensitiveAnnotatedParametersInference2.ts, 28, 9)) +>Foo : Symbol(Foo, Decl(contextSensitiveAnnotatedParametersInference2.ts, 9, 5)) + + params: { +>params : Symbol(params, Decl(contextSensitiveAnnotatedParametersInference2.ts, 28, 23)) + + foo: "boolean", +>foo : Symbol(foo, Decl(contextSensitiveAnnotatedParametersInference2.ts, 29, 11)) + + }, +}); + diff --git a/tests/baselines/reference/contextSensitiveAnnotatedParametersInference2.types b/tests/baselines/reference/contextSensitiveAnnotatedParametersInference2.types new file mode 100644 index 0000000000000..1b25763acb1a7 --- /dev/null +++ b/tests/baselines/reference/contextSensitiveAnnotatedParametersInference2.types @@ -0,0 +1,127 @@ +//// [tests/cases/compiler/contextSensitiveAnnotatedParametersInference2.ts] //// + +=== contextSensitiveAnnotatedParametersInference2.ts === +// https://github.com/microsoft/TypeScript/issues/60047 + +type Map = { +>Map : Map +> : ^^^^^^ + + [P in keyof T]: T[P] extends boolean ? "boolean" : "other"; +}; + +export function buildCommand>(builderArgs: { +>buildCommand : >(builderArgs: { func: (p: F) => void; params: Map>; }) => void +> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^^^^^ +>builderArgs : { func: (p: F) => void; params: Map>; } +> : ^^^^^^^^ ^^^^^^^^^^ ^^^ + + func: (p: F) => void; +>func : (p: F) => void +> : ^ ^^ ^^^^^ +>p : F +> : ^ + + params: Map>; +>params : Map> +> : ^^^^^^^^^^^^^^^ + +}) {} + +type Foo = { foo: boolean }; +>Foo : Foo +> : ^^^ +>foo : boolean +> : ^^^^^^^ + +buildCommand({ +>buildCommand({ func: function (p: Foo) {}, params: { foo: "boolean", },}) : void +> : ^^^^ +>buildCommand : >(builderArgs: { func: (p: F) => void; params: Map>; }) => void +> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^^^^^ +>{ func: function (p: Foo) {}, params: { foo: "boolean", },} : { func: (p: Foo) => void; params: { foo: "boolean"; }; } +> : ^^^^^^^^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + func: function (p: Foo) {}, +>func : (p: Foo) => void +> : ^ ^^ ^^^^^^^^^ +>function (p: Foo) {} : (p: Foo) => void +> : ^ ^^ ^^^^^^^^^ +>p : Foo +> : ^^^ + + params: { +>params : { foo: "boolean"; } +> : ^^^^^^^^^^^^^^^^^^^ +>{ foo: "boolean", } : { foo: "boolean"; } +> : ^^^^^^^^^^^^^^^^^^^ + + foo: "boolean", +>foo : "boolean" +> : ^^^^^^^^^ +>"boolean" : "boolean" +> : ^^^^^^^^^ + + }, +}); + +buildCommand({ +>buildCommand({ func(p: Foo) {}, params: { foo: "boolean", },}) : void +> : ^^^^ +>buildCommand : >(builderArgs: { func: (p: F) => void; params: Map>; }) => void +> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^^^^^ +>{ func(p: Foo) {}, params: { foo: "boolean", },} : { func(p: Foo): void; params: { foo: "boolean"; }; } +> : ^^^^^^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + func(p: Foo) {}, +>func : (p: Foo) => void +> : ^ ^^ ^^^^^^^^^ +>p : Foo +> : ^^^ + + params: { +>params : { foo: "boolean"; } +> : ^^^^^^^^^^^^^^^^^^^ +>{ foo: "boolean", } : { foo: "boolean"; } +> : ^^^^^^^^^^^^^^^^^^^ + + foo: "boolean", +>foo : "boolean" +> : ^^^^^^^^^ +>"boolean" : "boolean" +> : ^^^^^^^^^ + + }, +}); + +buildCommand({ +>buildCommand({ func: (p: Foo) => {}, params: { foo: "boolean", },}) : void +> : ^^^^ +>buildCommand : >(builderArgs: { func: (p: F) => void; params: Map>; }) => void +> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^^^^^ +>{ func: (p: Foo) => {}, params: { foo: "boolean", },} : { func: (p: Foo) => void; params: { foo: "boolean"; }; } +> : ^^^^^^^^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + func: (p: Foo) => {}, +>func : (p: Foo) => void +> : ^ ^^ ^^^^^^^^^ +>(p: Foo) => {} : (p: Foo) => void +> : ^ ^^ ^^^^^^^^^ +>p : Foo +> : ^^^ + + params: { +>params : { foo: "boolean"; } +> : ^^^^^^^^^^^^^^^^^^^ +>{ foo: "boolean", } : { foo: "boolean"; } +> : ^^^^^^^^^^^^^^^^^^^ + + foo: "boolean", +>foo : "boolean" +> : ^^^^^^^^^ +>"boolean" : "boolean" +> : ^^^^^^^^^ + + }, +}); + diff --git a/tests/cases/compiler/contextSensitiveAnnotatedParametersInference.ts b/tests/cases/compiler/contextSensitiveAnnotatedParametersInference1.ts similarity index 100% rename from tests/cases/compiler/contextSensitiveAnnotatedParametersInference.ts rename to tests/cases/compiler/contextSensitiveAnnotatedParametersInference1.ts diff --git a/tests/cases/compiler/contextSensitiveAnnotatedParametersInference2.ts b/tests/cases/compiler/contextSensitiveAnnotatedParametersInference2.ts new file mode 100644 index 0000000000000..6aa088c3636e8 --- /dev/null +++ b/tests/cases/compiler/contextSensitiveAnnotatedParametersInference2.ts @@ -0,0 +1,36 @@ +// @strict: true +// @noEmit: true + +// https://github.com/microsoft/TypeScript/issues/60047 + +type Map = { + [P in keyof T]: T[P] extends boolean ? "boolean" : "other"; +}; + +export function buildCommand>(builderArgs: { + func: (p: F) => void; + params: Map>; +}) {} + +type Foo = { foo: boolean }; + +buildCommand({ + func: function (p: Foo) {}, + params: { + foo: "boolean", + }, +}); + +buildCommand({ + func(p: Foo) {}, + params: { + foo: "boolean", + }, +}); + +buildCommand({ + func: (p: Foo) => {}, + params: { + foo: "boolean", + }, +}); From e34897d63273309e88cab78efdd059ba7fe0ffa8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Sun, 1 Dec 2024 22:07:49 +0100 Subject: [PATCH 3/3] add extra completion test --- ...ferenceSourceInPartiallyAnnotatedSignature1.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 tests/cases/fourslash/completionsObjectLiteralInferenceSourceInPartiallyAnnotatedSignature1.ts diff --git a/tests/cases/fourslash/completionsObjectLiteralInferenceSourceInPartiallyAnnotatedSignature1.ts b/tests/cases/fourslash/completionsObjectLiteralInferenceSourceInPartiallyAnnotatedSignature1.ts new file mode 100644 index 0000000000000..eda003f839f07 --- /dev/null +++ b/tests/cases/fourslash/completionsObjectLiteralInferenceSourceInPartiallyAnnotatedSignature1.ts @@ -0,0 +1,15 @@ +/// + +// @strict: true +// @lib: esnext + +//// declare function registerPlugin>( +//// callback: (app: any, options: Opt) => void, +//// opts: NoInfer, +//// ): Promise; +//// +//// registerPlugin((app, opts: { something: string }) => {}, { +//// /*1*/ +//// }); + +verify.completions({ marker: "1", includes: [`something`] });