Skip to content

@typedef in JS comment crashes typescript compiler #18097

Description

@TimvdLippe

Running tsc on a project which allows JS files and it has a JS file with a @typedef comment crashes the compiler.
The actual project is a lot bigger and I also encountered the following error:

TypeError: Cannot read property 'kind' of undefined
    at isIdentifierThatStartsWithUnderScore (node_modules/typescript/lib/typescript.js:44501:24)
    at errorUnusedLocal (node_modules/typescript/lib/typescript.js:44487:17)
    at node_modules/typescript/lib/typescript.js:44551:33
    at Map.forEach (native)
    at checkUnusedModuleMembers (node_modules/typescript/lib/typescript.js:44546:29)
    at checkUnusedIdentifiers (node_modules/typescript/lib/typescript.js:44417:29)
    at checkSourceFileWorker (node_modules/typescript/lib/typescript.js:46868:21)
    at checkSourceFile (node_modules/typescript/lib/typescript.js:46842:13)
    at Object.forEach (node_modules/typescript/lib/typescript.js:1506:30)
    at getDiagnosticsWorker (node_modules/typescript/lib/typescript.js:46924:16)

Removing the @typedef from the comment fixes the compiler crash as well.

Apart from the minimal reproducible case, I encountered the actual crash on the following snippet:

/**
 * @typedef {{
 *   _templatizerTemplate: HTMLTemplateElement,
 *   _parentModel: boolean,
 *   _instanceProps: Object,
 *   _forwardHostPropV2: Function,
 *   _notifyInstancePropV2: Function,
 *   ctor: TemplateInstanceBase
 * }}
 */
let TemplatizerUser;  // eslint-disable-line

TypeScript Version: 2.4.2

Code

src/index.ts

import './export.js'

src/export.js

/**
 * @typedef {{
 * }}
 */
export const foo = 5;

tsconfig.json

{
  "compilerOptions": {
    "target": "es6",
    "lib": [
      "es2016",
      "esnext.asynciterable",
      "dom"
    ],
    "allowJs": true,
    "module": "commonjs",
    "moduleResolution": "node",
    "isolatedModules": false,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitAny": true,
    "removeComments": false,
    "preserveConstEnums": true,
    "suppressImplicitAnyIndexErrors": false,
    "strict": true,
    "strictNullChecks": false,
    "outDir": "lib",
    "rootDir": "src"
  },
  "include": [
    "src/**/*"
  ]
}

Expected behavior:
No compiler crash.

Actual behavior:

TypeError: Cannot read property 'end' of undefined
    at getFullWidth (/global/node_modules/typescript/lib/tsc.js:5106:20)
    at Object.declarationNameToString (/global/node_modules/typescript/lib/tsc.js:5400:16)
    at checkExportsOnMergedDeclarations (/global/node_modules/typescript/lib/tsc.js:36417:138)
    at checkVariableLikeDeclaration (/global/node_modules/typescript/lib/tsc.js:37184:17)
    at checkVariableDeclaration (/global/node_modules/typescript/lib/tsc.js:37213:20)
    at checkSourceElement (/global/node_modules/typescript/lib/tsc.js:38763:28)
    at Object.forEach (/global/node_modules/typescript/lib/tsc.js:298:30)
    at checkVariableStatement (/global/node_modules/typescript/lib/tsc.js:37221:16)
    at checkSourceElement (/global/node_modules/typescript/lib/tsc.js:38732:28)
    at Object.forEach (/global/node_modules/typescript/lib/tsc.js:298:30)

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions