Skip to content

Infer typedef variable declaration to never #36454

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

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 8 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7084,6 +7084,14 @@ namespace ts {
return addOptionality(declaredType, isOptional);
}

const jsDocTypeDefTag = getJSDocTypedefTag(declaration);
// The typedef is attached to a variable declaration, instead of specifying the name in the typedef itself
// This means that the variable declared references the typedef and should not be used in regular control
// flow of the program, only in jsdoc type references.
if (jsDocTypeDefTag && !jsDocTypeDefTag.name && isVariableDeclaration(declaration)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably need to check that there is no initialiser too

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, and isInJSFile, no exports, not a binding pattern, not a declare (which is illegal in js, but might show up anyway).

return neverType;
}

if ((noImplicitAny || isInJSFile(declaration)) &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, maybe the new code should be moved inside here.

declaration.kind === SyntaxKind.VariableDeclaration && !isBindingPattern(declaration.name) &&
!(getCombinedModifierFlags(declaration) & ModifierFlags.Export) && !(declaration.flags & NodeFlags.Ambient)) {
Expand Down
5 changes: 5 additions & 0 deletions src/compiler/utilitiesPublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,11 @@ namespace ts {
return getFirstJSDocTag(node, isJSDocTemplateTag);
}

/** Gets the JSDoc typedef tag for the node if present */
export function getJSDocTypedefTag(node: Node): JSDocTypedefTag | undefined {
return getFirstJSDocTag(node, isJSDocTypedefTag);
}

/** Gets the JSDoc type tag for the node if present and valid */
export function getJSDocTypeTag(node: Node): JSDocTypeTag | undefined {
// We should have already issued an error if there were multiple type jsdocs, so just use the first one.
Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3524,6 +3524,8 @@ declare namespace ts {
function getJSDocReturnTag(node: Node): JSDocReturnTag | undefined;
/** Gets the JSDoc template tag for the node if present */
function getJSDocTemplateTag(node: Node): JSDocTemplateTag | undefined;
/** Gets the JSDoc typedef tag for the node if present */
function getJSDocTypedefTag(node: Node): JSDocTypedefTag | undefined;
/** Gets the JSDoc type tag for the node if present and valid */
function getJSDocTypeTag(node: Node): JSDocTypeTag | undefined;
/**
Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3524,6 +3524,8 @@ declare namespace ts {
function getJSDocReturnTag(node: Node): JSDocReturnTag | undefined;
/** Gets the JSDoc template tag for the node if present */
function getJSDocTemplateTag(node: Node): JSDocTemplateTag | undefined;
/** Gets the JSDoc typedef tag for the node if present */
function getJSDocTypedefTag(node: Node): JSDocTypedefTag | undefined;
/** Gets the JSDoc type tag for the node if present and valid */
function getJSDocTypeTag(node: Node): JSDocTypeTag | undefined;
/**
Expand Down
9 changes: 9 additions & 0 deletions tests/baselines/reference/jsdocTypeDefNoImplicitAny.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
=== tests/cases/conformance/jsdoc/index.js ===
/** @typedef {{foo: number}} */
export let a;
>a : Symbol(a, Decl(index.js, 1, 10), Decl(index.js, 0, 4))

/** @typedef {number} */
let b;
>b : Symbol(b, Decl(index.js, 3, 3), Decl(index.js, 2, 4))

9 changes: 9 additions & 0 deletions tests/baselines/reference/jsdocTypeDefNoImplicitAny.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
=== tests/cases/conformance/jsdoc/index.js ===
/** @typedef {{foo: number}} */
export let a;
>a : never

/** @typedef {number} */
let b;
>b : never

2 changes: 1 addition & 1 deletion tests/baselines/reference/jsdocTypedefNoCrash.types
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
* }}
*/
export const foo = 5;
>foo : 5
>foo : never
>5 : 5

2 changes: 1 addition & 1 deletion tests/baselines/reference/jsdocTypedefNoCrash2.types
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ export type foo = 5;
* }}
*/
export const foo = 5;
>foo : 5
>foo : never
>5 : 5

9 changes: 9 additions & 0 deletions tests/cases/conformance/jsdoc/jsdocTypeDefNoImplicitAny.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// @allowJs: true
// @checkJs: true
// @noEmit: true
// @noImplicitAny: true
// @Filename: index.js
/** @typedef {{foo: number}} */
export let a;
/** @typedef {number} */
let b;
2 changes: 1 addition & 1 deletion tests/cases/fourslash/jsdocTypedefTagSemanticMeaning1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
/////** @type {[|T|]} */
////const n = [|T|];

verify.singleReferenceGroup("type T = number\nconst T: 1", "T");
verify.singleReferenceGroup("type T = number\nconst T: never", "T");