Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(60908): Unexpected "'Type' is declared but its value is never read." error with jsdoc @import syntax #60921

Merged
merged 7 commits into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3948,6 +3948,10 @@ export function getContainerFlags(node: Node): ContainerFlags {
case SyntaxKind.ClassStaticBlockDeclaration:
return ContainerFlags.IsContainer | ContainerFlags.IsControlFlowContainer | ContainerFlags.HasLocals | ContainerFlags.IsFunctionLike;

case SyntaxKind.JSDocImportTag:
// treat as a container to prevent using an enclosing effective host, ensuring import bindings are scoped correctly
return ContainerFlags.IsContainer | ContainerFlags.IsControlFlowContainer | ContainerFlags.HasLocals;

case SyntaxKind.FunctionExpression:
case SyntaxKind.ArrowFunction:
return ContainerFlags.IsContainer | ContainerFlags.IsControlFlowContainer | ContainerFlags.HasLocals | ContainerFlags.IsFunctionLike | ContainerFlags.IsFunctionExpression;
Expand Down
38 changes: 38 additions & 0 deletions tests/baselines/reference/importTag24.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
a.js(3,10): error TS6133: 'f1' is declared but its value is never read.
a.js(11,10): error TS6133: 'f3' is declared but its value is never read.
a.js(19,10): error TS6133: 'f4' is declared but its value is never read.
a.js(19,17): error TS2322: Type 'number' is not assignable to type 'string'.


==== types.d.ts (0 errors) ====
export type Foo = string;

==== a.js (4 errors) ====
/** @import { Foo } from './types.d.ts' */

function f1() { return undefined; }
~~
!!! error TS6133: 'f1' is declared but its value is never read.

export function f2() {
/** @type {Set<Foo>} */
const foo = new Set([ 'a', 'b' ]);
return foo;
}

function f3() { return undefined; }
~~
!!! error TS6133: 'f3' is declared but its value is never read.

/** @type {Set<Foo>} */
export const foo = new Set([ 'a', 'b' ]);

/**
* @returns {Foo}
*/
function f4() { return 1; }
~~
!!! error TS6133: 'f4' is declared but its value is never read.
~~~~~~
!!! error TS2322: Type 'number' is not assignable to type 'string'.

40 changes: 40 additions & 0 deletions tests/baselines/reference/importTag24.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//// [tests/cases/conformance/jsdoc/importTag24.ts] ////

=== types.d.ts ===
export type Foo = string;
>Foo : Symbol(Foo, Decl(types.d.ts, 0, 0))

=== a.js ===
/** @import { Foo } from './types.d.ts' */

function f1() { return undefined; }
>f1 : Symbol(f1, Decl(a.js, 0, 0))
>undefined : Symbol(undefined)

export function f2() {
>f2 : Symbol(f2, Decl(a.js, 2, 35))

/** @type {Set<Foo>} */
const foo = new Set([ 'a', 'b' ]);
>foo : Symbol(foo, Decl(a.js, 6, 9))
>Set : Symbol(Set, Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.esnext.collection.d.ts, --, --))

return foo;
>foo : Symbol(foo, Decl(a.js, 6, 9))
}

function f3() { return undefined; }
>f3 : Symbol(f3, Decl(a.js, 8, 1))
>undefined : Symbol(undefined)

/** @type {Set<Foo>} */
export const foo = new Set([ 'a', 'b' ]);
>foo : Symbol(foo, Decl(a.js, 13, 12))
>Set : Symbol(Set, Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.esnext.collection.d.ts, --, --))

/**
* @returns {Foo}
*/
function f4() { return 1; }
>f4 : Symbol(f4, Decl(a.js, 13, 41))

74 changes: 74 additions & 0 deletions tests/baselines/reference/importTag24.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//// [tests/cases/conformance/jsdoc/importTag24.ts] ////

=== Performance Stats ===
Type Count: 1,000
Instantiation count: 2,500

=== types.d.ts ===
export type Foo = string;
>Foo : string
> : ^^^^^^

=== a.js ===
/** @import { Foo } from './types.d.ts' */

function f1() { return undefined; }
>f1 : () => any
> : ^^^^^^^^^
>undefined : undefined
> : ^^^^^^^^^

export function f2() {
>f2 : () => Set<string>
> : ^^^^^^^^^^^^^^^^^

/** @type {Set<Foo>} */
const foo = new Set([ 'a', 'b' ]);
>foo : Set<string>
> : ^^^^^^^^^^^
>new Set([ 'a', 'b' ]) : Set<string>
> : ^^^^^^^^^^^
>Set : SetConstructor
> : ^^^^^^^^^^^^^^
>[ 'a', 'b' ] : string[]
> : ^^^^^^^^
>'a' : "a"
> : ^^^
>'b' : "b"
> : ^^^

return foo;
>foo : Set<string>
> : ^^^^^^^^^^^
}

function f3() { return undefined; }
>f3 : () => any
> : ^^^^^^^^^
>undefined : undefined
> : ^^^^^^^^^

/** @type {Set<Foo>} */
export const foo = new Set([ 'a', 'b' ]);
>foo : Set<string>
> : ^^^^^^^^^^^
>new Set([ 'a', 'b' ]) : Set<string>
> : ^^^^^^^^^^^
>Set : SetConstructor
> : ^^^^^^^^^^^^^^
>[ 'a', 'b' ] : string[]
> : ^^^^^^^^
>'a' : "a"
> : ^^^
>'b' : "b"
> : ^^^

/**
* @returns {Foo}
*/
function f4() { return 1; }
>f4 : () => Foo
> : ^^^^^^^^^
>1 : 1
> : ^

25 changes: 25 additions & 0 deletions tests/baselines/reference/importTag25.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//// [tests/cases/conformance/jsdoc/importTag25.ts] ////

=== types.d.ts ===
export type T = {
>T : Symbol(T, Decl(types.d.ts, 0, 0))

a: number;
>a : Symbol(a, Decl(types.d.ts, 0, 17))

};

=== foo.js ===
/** @import { T } from "./types.d.ts" */

export default async function f() {
>f : Symbol(f, Decl(foo.js, 0, 0))

/** @type {T[]} */
const types = [];
>types : Symbol(types, Decl(foo.js, 4, 6))

return types;
>types : Symbol(types, Decl(foo.js, 4, 6))
}

32 changes: 32 additions & 0 deletions tests/baselines/reference/importTag25.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//// [tests/cases/conformance/jsdoc/importTag25.ts] ////

=== types.d.ts ===
export type T = {
>T : T
> : ^

a: number;
>a : number
> : ^^^^^^

};

=== foo.js ===
/** @import { T } from "./types.d.ts" */

export default async function f() {
>f : () => Promise<T[]>
> : ^^^^^^^^^^^^^^^^^^

/** @type {T[]} */
const types = [];
>types : T[]
> : ^^^
>[] : undefined[]
> : ^^^^^^^^^^^

return types;
>types : T[]
> : ^^^
}

33 changes: 33 additions & 0 deletions tests/cases/conformance/jsdoc/importTag24.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// @checkJs: true
// @allowJs: true
// @noEmit: true
// @lib: esnext
// @moduleResolution: bundler
// @module: preserve
// @noUnusedLocals: true
// @noUnusedParameters: true
// @allowImportingTsExtensions: true

// @filename: types.d.ts
export type Foo = string;

// @filename: a.js
/** @import { Foo } from './types.d.ts' */

function f1() { return undefined; }

export function f2() {
/** @type {Set<Foo>} */
const foo = new Set([ 'a', 'b' ]);
return foo;
}

function f3() { return undefined; }

/** @type {Set<Foo>} */
export const foo = new Set([ 'a', 'b' ]);

/**
* @returns {Foo}
*/
function f4() { return 1; }
18 changes: 18 additions & 0 deletions tests/cases/conformance/jsdoc/importTag25.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// @noUnusedLocals: true
// @allowJs: true
// @checkJs: true
// @noEmit: true

// @filename: types.d.ts
export type T = {
a: number;
};

// @filename: foo.js
/** @import { T } from "./types.d.ts" */

export default async function f() {
/** @type {T[]} */
const types = [];
return types;
}