Skip to content

Commit 8223c07

Browse files
authored
getTypeFromJSDocValueReference: handle import types (microsoft#34683)
Previously it only handled types whose declaration was from `require`, but now it handles types whose reference is an import type as well.
1 parent eb08ee6 commit 8223c07

File tree

4 files changed

+74
-2
lines changed

4 files changed

+74
-2
lines changed

src/compiler/checker.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -10754,7 +10754,7 @@ namespace ts {
1075410754
/**
1075510755
* A JSdoc TypeReference may be to a value, but resolve it as a type anyway.
1075610756
* Note: If the value is imported from commonjs, it should really be an alias,
10757-
* but this function fakes special-case code fakes alias resolution as well.
10757+
* but this function's special-case code fakes alias resolution as well.
1075810758
*/
1075910759
function getTypeFromJSDocValueReference(node: NodeWithTypeArguments, symbol: Symbol): Type | undefined {
1076010760
const valueType = getTypeOfSymbol(symbol);
@@ -10766,7 +10766,7 @@ namespace ts {
1076610766
&& isCallExpression(decl.initializer)
1076710767
&& isRequireCall(decl.initializer, /*requireStringLiteralLikeArgument*/ true)
1076810768
&& valueType.symbol;
10769-
if (isRequireAlias) {
10769+
if (isRequireAlias || node.kind === SyntaxKind.ImportType) {
1077010770
typeType = getTypeReferenceType(node, valueType.symbol);
1077110771
}
1077210772
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
=== tests/cases/conformance/jsdoc/mod1.js ===
2+
class C {
3+
>C : Symbol(C, Decl(mod1.js, 0, 0))
4+
5+
s() { }
6+
>s : Symbol(C.s, Decl(mod1.js, 0, 9))
7+
}
8+
module.exports.C = C
9+
>module.exports.C : Symbol(C, Decl(mod1.js, 2, 1))
10+
>module.exports : Symbol(C, Decl(mod1.js, 2, 1))
11+
>module : Symbol(module, Decl(mod1.js, 2, 1))
12+
>exports : Symbol("tests/cases/conformance/jsdoc/mod1", Decl(mod1.js, 0, 0))
13+
>C : Symbol(C, Decl(mod1.js, 2, 1))
14+
>C : Symbol(C, Decl(mod1.js, 0, 0))
15+
16+
=== tests/cases/conformance/jsdoc/test.js ===
17+
/** @typedef {import('./mod1').C} X */
18+
/** @param {X} c */
19+
function demo(c) {
20+
>demo : Symbol(demo, Decl(test.js, 0, 0))
21+
>c : Symbol(c, Decl(test.js, 2, 14))
22+
23+
c.s
24+
>c.s : Symbol(C.s, Decl(mod1.js, 0, 9))
25+
>c : Symbol(c, Decl(test.js, 2, 14))
26+
>s : Symbol(C.s, Decl(mod1.js, 0, 9))
27+
}
28+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
=== tests/cases/conformance/jsdoc/mod1.js ===
2+
class C {
3+
>C : C
4+
5+
s() { }
6+
>s : () => void
7+
}
8+
module.exports.C = C
9+
>module.exports.C = C : typeof C
10+
>module.exports.C : typeof C
11+
>module.exports : typeof import("tests/cases/conformance/jsdoc/mod1")
12+
>module : { "tests/cases/conformance/jsdoc/mod1": typeof import("tests/cases/conformance/jsdoc/mod1"); }
13+
>exports : typeof import("tests/cases/conformance/jsdoc/mod1")
14+
>C : typeof C
15+
>C : typeof C
16+
17+
=== tests/cases/conformance/jsdoc/test.js ===
18+
/** @typedef {import('./mod1').C} X */
19+
/** @param {X} c */
20+
function demo(c) {
21+
>demo : (c: C) => void
22+
>c : C
23+
24+
c.s
25+
>c.s : () => void
26+
>c : C
27+
>s : () => void
28+
}
29+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// @noEmit: true
2+
// @allowJs: true
3+
// @checkJs: true
4+
// @Filename: mod1.js
5+
class C {
6+
s() { }
7+
}
8+
module.exports.C = C
9+
10+
// @Filename: test.js
11+
/** @typedef {import('./mod1').C} X */
12+
/** @param {X} c */
13+
function demo(c) {
14+
c.s
15+
}

0 commit comments

Comments
 (0)