diff --git a/internal/checker/nodebuilderimpl.go b/internal/checker/nodebuilderimpl.go index b1e539d621..2eda08fd60 100644 --- a/internal/checker/nodebuilderimpl.go +++ b/internal/checker/nodebuilderimpl.go @@ -2160,7 +2160,15 @@ func (b *NodeBuilderImpl) indexInfoToIndexSignatureDeclarationHelper(indexInfo * } func hasTypeAnnotation(declaration *ast.Declaration) bool { - return declaration != nil && declaration.Type() != nil + if declaration == nil || declaration.Type() == nil { + return false + } + // Type alias declarations have a .Type() that is their type definition, not a type annotation on a value. + // Exclude them so callers don't mistake them for annotated value declarations. + if ast.IsTypeAliasDeclaration(declaration) || ast.IsJSTypeAliasDeclaration(declaration) { + return false + } + return true } /** diff --git a/testdata/baselines/reference/compiler/jsTypedefMergedWithModuleExportProperty.errors.txt b/testdata/baselines/reference/compiler/jsTypedefMergedWithModuleExportProperty.errors.txt new file mode 100644 index 0000000000..518283e1cb --- /dev/null +++ b/testdata/baselines/reference/compiler/jsTypedefMergedWithModuleExportProperty.errors.txt @@ -0,0 +1,20 @@ +local-lib/ModuleGraphConnection.js(3,1): error TS2309: An export assignment cannot be used in a module with other exported elements. +local-lib/ModuleGraphConnection.js(4,16): error TS2339: Property 'T' does not exist on type 'typeof ModuleGraphConnection'. + + +==== local-lib/ModuleGraphConnection.js (2 errors) ==== + /** @typedef {typeof T} T */ + const T = Symbol(); + module.exports = class ModuleGraphConnection {}; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2309: An export assignment cannot be used in a module with other exported elements. + module.exports.T = T; + ~ +!!! error TS2339: Property 'T' does not exist on type 'typeof ModuleGraphConnection'. + +==== repro.js (0 errors) ==== + 'use strict'; + /** @typedef {import('./local-lib/ModuleGraphConnection')} ImportedType */ + /** @type {ImportedType} */ + module.exports = class Repro {}; + \ No newline at end of file diff --git a/testdata/baselines/reference/compiler/jsTypedefMergedWithModuleExportProperty.js b/testdata/baselines/reference/compiler/jsTypedefMergedWithModuleExportProperty.js new file mode 100644 index 0000000000..9708ba8ee6 --- /dev/null +++ b/testdata/baselines/reference/compiler/jsTypedefMergedWithModuleExportProperty.js @@ -0,0 +1,48 @@ +//// [tests/cases/compiler/jsTypedefMergedWithModuleExportProperty.ts] //// + +//// [ModuleGraphConnection.js] +/** @typedef {typeof T} T */ +const T = Symbol(); +module.exports = class ModuleGraphConnection {}; +module.exports.T = T; + +//// [repro.js] +'use strict'; +/** @typedef {import('./local-lib/ModuleGraphConnection')} ImportedType */ +/** @type {ImportedType} */ +module.exports = class Repro {}; + + +//// [ModuleGraphConnection.js] +"use strict"; +/** @typedef {typeof T} T */ +const T = Symbol(); +module.exports = class ModuleGraphConnection { +}; +module.exports.T = T; +//// [repro.js] +'use strict'; +/** @typedef {import('./local-lib/ModuleGraphConnection')} ImportedType */ +/** @type {ImportedType} */ +module.exports = class Repro { +}; + + +//// [ModuleGraphConnection.d.ts] +export = ModuleGraphConnection; +declare class ModuleGraphConnection { +} +declare namespace ModuleGraphConnection { + const _exported: typeof T; + export { _exported as T }; +} +export type T = typeof T; +/** @typedef {typeof T} T */ +declare const T: unique symbol; +//// [repro.d.ts] +export = Repro; +/** @typedef {import('./local-lib/ModuleGraphConnection')} ImportedType */ +/** @type {ImportedType} */ +declare class Repro { +} +export type ImportedType = import('./local-lib/ModuleGraphConnection'); diff --git a/testdata/baselines/reference/compiler/jsTypedefMergedWithModuleExportProperty.symbols b/testdata/baselines/reference/compiler/jsTypedefMergedWithModuleExportProperty.symbols new file mode 100644 index 0000000000..5cd1901280 --- /dev/null +++ b/testdata/baselines/reference/compiler/jsTypedefMergedWithModuleExportProperty.symbols @@ -0,0 +1,30 @@ +//// [tests/cases/compiler/jsTypedefMergedWithModuleExportProperty.ts] //// + +=== local-lib/ModuleGraphConnection.js === +/** @typedef {typeof T} T */ +const T = Symbol(); +>T : Symbol(T, Decl(ModuleGraphConnection.js, 1, 5), Decl(ModuleGraphConnection.js, 0, 4)) +>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --)) + +module.exports = class ModuleGraphConnection {}; +>module.exports : Symbol(exports, Decl(ModuleGraphConnection.js, 0, 0)) +>module : Symbol(module, Decl(ModuleGraphConnection.js, 0, 0)) +>exports : Symbol(exports, Decl(ModuleGraphConnection.js, 0, 0)) +>ModuleGraphConnection : Symbol(ModuleGraphConnection, Decl(ModuleGraphConnection.js, 2, 16)) + +module.exports.T = T; +>module.exports : Symbol(exports, Decl(ModuleGraphConnection.js, 0, 0)) +>module : Symbol(module, Decl(ModuleGraphConnection.js, 0, 0)) +>exports : Symbol(exports, Decl(ModuleGraphConnection.js, 0, 0)) +>T : Symbol(T, Decl(ModuleGraphConnection.js, 1, 5), Decl(ModuleGraphConnection.js, 0, 4)) + +=== repro.js === +'use strict'; +/** @typedef {import('./local-lib/ModuleGraphConnection')} ImportedType */ +/** @type {ImportedType} */ +module.exports = class Repro {}; +>module.exports : Symbol(exports, Decl(repro.js, 0, 0)) +>module : Symbol(module, Decl(repro.js, 0, 0)) +>exports : Symbol(exports, Decl(repro.js, 0, 0)) +>Repro : Symbol(Repro, Decl(repro.js, 3, 16)) + diff --git a/testdata/baselines/reference/compiler/jsTypedefMergedWithModuleExportProperty.types b/testdata/baselines/reference/compiler/jsTypedefMergedWithModuleExportProperty.types new file mode 100644 index 0000000000..eae466ce5f --- /dev/null +++ b/testdata/baselines/reference/compiler/jsTypedefMergedWithModuleExportProperty.types @@ -0,0 +1,40 @@ +//// [tests/cases/compiler/jsTypedefMergedWithModuleExportProperty.ts] //// + +=== local-lib/ModuleGraphConnection.js === +/** @typedef {typeof T} T */ +const T = Symbol(); +>T : unique symbol +>Symbol() : unique symbol +>Symbol : SymbolConstructor + +module.exports = class ModuleGraphConnection {}; +>module.exports = class ModuleGraphConnection {} : typeof ModuleGraphConnection +>module.exports : typeof ModuleGraphConnection +>module : { exports: typeof ModuleGraphConnection; } +>exports : typeof ModuleGraphConnection +>class ModuleGraphConnection {} : typeof ModuleGraphConnection +>ModuleGraphConnection : typeof ModuleGraphConnection + +module.exports.T = T; +>module.exports.T = T : unique symbol +>module.exports.T : any +>module.exports : typeof ModuleGraphConnection +>module : { exports: typeof ModuleGraphConnection; } +>exports : typeof ModuleGraphConnection +>T : any +>T : unique symbol + +=== repro.js === +'use strict'; +>'use strict' : "use strict" + +/** @typedef {import('./local-lib/ModuleGraphConnection')} ImportedType */ +/** @type {ImportedType} */ +module.exports = class Repro {}; +>module.exports = class Repro {} : typeof Repro +>module.exports : typeof Repro +>module : { exports: typeof Repro; } +>exports : typeof Repro +>class Repro {} : typeof Repro +>Repro : typeof Repro + diff --git a/testdata/tests/cases/compiler/jsTypedefMergedWithModuleExportProperty.ts b/testdata/tests/cases/compiler/jsTypedefMergedWithModuleExportProperty.ts new file mode 100644 index 0000000000..2aa7e887f6 --- /dev/null +++ b/testdata/tests/cases/compiler/jsTypedefMergedWithModuleExportProperty.ts @@ -0,0 +1,17 @@ +// @allowJs: true +// @checkJs: true +// @target: es2015 +// @outDir: ./out +// @declaration: true + +// @filename: local-lib/ModuleGraphConnection.js +/** @typedef {typeof T} T */ +const T = Symbol(); +module.exports = class ModuleGraphConnection {}; +module.exports.T = T; + +// @filename: repro.js +'use strict'; +/** @typedef {import('./local-lib/ModuleGraphConnection')} ImportedType */ +/** @type {ImportedType} */ +module.exports = class Repro {};