diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 52a667a14514f..578a3ffc84e49 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -11616,9 +11616,16 @@ namespace ts { } isRequireAlias = isCallExpression(expr) && isRequireCall(expr, /*requireStringLiteralLikeArgument*/ true) && !!valueType.symbol; } - const isImportTypeWithQualifier = node.kind === SyntaxKind.ImportType && (node as ImportTypeNode).qualifier; + + // We need to do an extra hop to find the correct type when type being referred is a class, because + // a class and its type are interwoven in the type, see jsdocImportTypeReferenceToClassAlias for + // an example of it in action + const isImportOfClassTypeWithQualifier = node.kind === SyntaxKind.ImportType + && (node as ImportTypeNode).qualifier + && valueType.symbol.flags & SymbolFlags.Class; // valueType might not have a symbol, eg, {import('./b').STRING_LITERAL} - if (valueType.symbol && (isRequireAlias || isImportTypeWithQualifier)) { + if (valueType.symbol && (isRequireAlias || isImportOfClassTypeWithQualifier)) { + // follow fake alias (commonjs or class value->type) typeType = getTypeReferenceType(node, valueType.symbol); } } diff --git a/tests/baselines/reference/jsDocImplicitTypeOfForValueImport.symbols b/tests/baselines/reference/jsDocImplicitTypeOfForValueImport.symbols new file mode 100644 index 0000000000000..a3abf4774f1c3 --- /dev/null +++ b/tests/baselines/reference/jsDocImplicitTypeOfForValueImport.symbols @@ -0,0 +1,15 @@ +=== tests/cases/compiler/lib/index.js === +// 36830 +// + +/** @type {import('../folder/index').zzz} */ +export function xxx() { +>xxx : Symbol(xxx, Decl(index.js, 0, 0)) + + return null; +} + +=== tests/cases/compiler/folder/index.d.ts === +export function zzz(): string; +>zzz : Symbol(zzz, Decl(index.d.ts, 0, 0)) + diff --git a/tests/baselines/reference/jsdocImportTypeReferenceToClassAlias.symbols b/tests/baselines/reference/jsdocImportTypeReferenceToClassAlias.symbols index c62d8e0676fa5..5f08204665854 100644 --- a/tests/baselines/reference/jsdocImportTypeReferenceToClassAlias.symbols +++ b/tests/baselines/reference/jsdocImportTypeReferenceToClassAlias.symbols @@ -21,8 +21,6 @@ function demo(c) { >c : Symbol(c, Decl(test.js, 2, 14)) c.s ->c.s : Symbol(C.s, Decl(mod1.js, 0, 9)) >c : Symbol(c, Decl(test.js, 2, 14)) ->s : Symbol(C.s, Decl(mod1.js, 0, 9)) } diff --git a/tests/baselines/reference/jsdocImportTypeReferenceToClassAlias.types b/tests/baselines/reference/jsdocImportTypeReferenceToClassAlias.types index 49b85f1f06361..8edd93653be46 100644 --- a/tests/baselines/reference/jsdocImportTypeReferenceToClassAlias.types +++ b/tests/baselines/reference/jsdocImportTypeReferenceToClassAlias.types @@ -19,11 +19,11 @@ module.exports.C = C /** @param {X} c */ function demo(c) { >demo : (c: X) => void ->c : C +>c : typeof C c.s ->c.s : () => void ->c : C ->s : () => void +>c.s : any +>c : typeof C +>s : any } diff --git a/tests/cases/compiler/jsDocImplicitTypeOfForValueImport.ts b/tests/cases/compiler/jsDocImplicitTypeOfForValueImport.ts new file mode 100644 index 0000000000000..9670ec615e9e9 --- /dev/null +++ b/tests/cases/compiler/jsDocImplicitTypeOfForValueImport.ts @@ -0,0 +1,14 @@ +// 36830 +// +// @allowJs: true +// @noEmit: true +// @checkJs: true + +// @filename: lib/index.js +/** @type {import('../folder/index').zzz} */ +export function xxx() { + return null; +} + +// @filename: folder/index.d.ts +export function zzz(): string; diff --git a/tests/cases/fourslash/fourslash.ts b/tests/cases/fourslash/fourslash.ts index d9b47adc1fb7a..542e8e63ba00f 100644 --- a/tests/cases/fourslash/fourslash.ts +++ b/tests/cases/fourslash/fourslash.ts @@ -28,7 +28,7 @@ //--------------------------------------- // For API editors: -// When editting this file, and only while editing this file, enable the reference comments +// When editing this file, and only while editing this file, enable the reference comments // and comment out the declarations in this section to get proper type information. // Undo these changes before compiling/committing/editing any other fourslash tests. // The test suite will likely crash if you try 'jake runtests' with reference comments enabled.