Skip to content

Commit 77cee24

Browse files
Andaristsnovader
authored andcommitted
Infer from parameters annotated using JSDoc (microsoft#55400)
1 parent 99e0cad commit 77cee24

File tree

4 files changed

+123
-7
lines changed

4 files changed

+123
-7
lines changed

src/compiler/checker.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35600,13 +35600,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3560035600
const len = signature.parameters.length - (signatureHasRestParameter(signature) ? 1 : 0);
3560135601
for (let i = 0; i < len; i++) {
3560235602
const declaration = signature.parameters[i].valueDeclaration as ParameterDeclaration;
35603-
if (declaration.type) {
35604-
const typeNode = getEffectiveTypeAnnotationNode(declaration);
35605-
if (typeNode) {
35606-
const source = addOptionality(getTypeFromTypeNode(typeNode), /*isProperty*/ false, isOptionalDeclaration(declaration));
35607-
const target = getTypeAtPosition(context, i);
35608-
inferTypes(inferenceContext.inferences, source, target);
35609-
}
35603+
const typeNode = getEffectiveTypeAnnotationNode(declaration);
35604+
if (typeNode) {
35605+
const source = addOptionality(getTypeFromTypeNode(typeNode), /*isProperty*/ false, isOptionalDeclaration(declaration));
35606+
const target = getTypeAtPosition(context, i);
35607+
inferTypes(inferenceContext.inferences, source, target);
3561035608
}
3561135609
}
3561235610
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//// [tests/cases/compiler/contravariantOnlyInferenceFromAnnotatedFunctionJs.ts] ////
2+
3+
=== index.js ===
4+
/**
5+
* @typedef {{ [K in keyof B]: { fn: (a: A, b: B) => void; thing: B[K]; } }} Funcs
6+
* @template A
7+
* @template {Record<string, unknown>} B
8+
*/
9+
10+
/**
11+
* @template A
12+
* @template {Record<string, unknown>} B
13+
* @param {Funcs<A, B>} fns
14+
* @returns {[A, B]}
15+
*/
16+
function foo(fns) {
17+
>foo : Symbol(foo, Decl(index.js, 0, 0))
18+
>fns : Symbol(fns, Decl(index.js, 12, 13))
19+
20+
return /** @type {any} */ (null);
21+
}
22+
23+
const result = foo({
24+
>result : Symbol(result, Decl(index.js, 16, 5))
25+
>foo : Symbol(foo, Decl(index.js, 0, 0))
26+
27+
bar: {
28+
>bar : Symbol(bar, Decl(index.js, 16, 20))
29+
30+
fn:
31+
>fn : Symbol(fn, Decl(index.js, 17, 8))
32+
33+
/** @param {string} a */
34+
(a) => {},
35+
>a : Symbol(a, Decl(index.js, 20, 7))
36+
37+
thing: "asd",
38+
>thing : Symbol(thing, Decl(index.js, 20, 16))
39+
40+
},
41+
});
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//// [tests/cases/compiler/contravariantOnlyInferenceFromAnnotatedFunctionJs.ts] ////
2+
3+
=== index.js ===
4+
/**
5+
* @typedef {{ [K in keyof B]: { fn: (a: A, b: B) => void; thing: B[K]; } }} Funcs
6+
* @template A
7+
* @template {Record<string, unknown>} B
8+
*/
9+
10+
/**
11+
* @template A
12+
* @template {Record<string, unknown>} B
13+
* @param {Funcs<A, B>} fns
14+
* @returns {[A, B]}
15+
*/
16+
function foo(fns) {
17+
>foo : <A, B extends Record<string, unknown>>(fns: Funcs<A, B>) => [A, B]
18+
>fns : Funcs<A, B>
19+
20+
return /** @type {any} */ (null);
21+
>(null) : any
22+
}
23+
24+
const result = foo({
25+
>result : [string, { bar: string; }]
26+
>foo({ bar: { fn: /** @param {string} a */ (a) => {}, thing: "asd", },}) : [string, { bar: string; }]
27+
>foo : <A, B extends Record<string, unknown>>(fns: Funcs<A, B>) => [A, B]
28+
>{ bar: { fn: /** @param {string} a */ (a) => {}, thing: "asd", },} : { bar: { fn: (a: string) => void; thing: string; }; }
29+
30+
bar: {
31+
>bar : { fn: (a: string) => void; thing: string; }
32+
>{ fn: /** @param {string} a */ (a) => {}, thing: "asd", } : { fn: (a: string) => void; thing: string; }
33+
34+
fn:
35+
>fn : (a: string) => void
36+
37+
/** @param {string} a */
38+
(a) => {},
39+
>(a) => {} : (a: string) => void
40+
>a : string
41+
42+
thing: "asd",
43+
>thing : string
44+
>"asd" : "asd"
45+
46+
},
47+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// @strict: true
2+
// @checkJs: true
3+
// @noEmit: true
4+
5+
// @filename: index.js
6+
7+
/**
8+
* @typedef {{ [K in keyof B]: { fn: (a: A, b: B) => void; thing: B[K]; } }} Funcs
9+
* @template A
10+
* @template {Record<string, unknown>} B
11+
*/
12+
13+
/**
14+
* @template A
15+
* @template {Record<string, unknown>} B
16+
* @param {Funcs<A, B>} fns
17+
* @returns {[A, B]}
18+
*/
19+
function foo(fns) {
20+
return /** @type {any} */ (null);
21+
}
22+
23+
const result = foo({
24+
bar: {
25+
fn:
26+
/** @param {string} a */
27+
(a) => {},
28+
thing: "asd",
29+
},
30+
});

0 commit comments

Comments
 (0)