Typescript: TypeError: Cannot read property 'valueDeclaration' of undefined

Created on 1 Nov 2019  ·  26Comments  ·  Source: microsoft/TypeScript

TypeScript Version: 3.8.0-dev.20191101

Running into this exception when trying to create declarations from .js files. Cannot use the Playground to illustrate this; it doesn't support .d.ts generation

Using tsconfig.json:

  {
    "compilerOptions": {
      "allowJs": true,
      "checkJs": true,
      "declaration": true,
      "emitDeclarationOnly": true
    },
    "include": ["foo.js"]
  }

Run tsc, which should create foo.d.ts. Run it _again_ to get the error.

Expected behavior:

a different error about refusing to overwrite .d.ts files

Actual behavior:

$ tsc
/Users/boneskull/projects/boneskull/sync-monorepo-packages/node_modules/typescript/lib/tsc.js:78600
                throw e;
                ^

TypeError: Cannot read property 'valueDeclaration' of undefined
    at getExpandoSymbol (/Users/boneskull/projects/boneskull/sync-monorepo-packages/node_modules/typescript/lib/tsc.js:28794:31)
    at getTypeReferenceType (/Users/boneskull/projects/boneskull/sync-monorepo-packages/node_modules/typescript/lib/tsc.js:35293:22)
    at getTypeFromJSDocValueReference (/Users/boneskull/projects/boneskull/sync-monorepo-packages/node_modules/typescript/lib/tsc.js:35332:32)
    at getTypeReferenceType (/Users/boneskull/projects/boneskull/sync-monorepo-packages/node_modules/typescript/lib/tsc.js:35307:33)
    at resolveImportSymbolType (/Users/boneskull/projects/boneskull/sync-monorepo-packages/node_modules/typescript/lib/tsc.js:36697:24)
    at getTypeFromImportTypeNode (/Users/boneskull/projects/boneskull/sync-monorepo-packages/node_modules/typescript/lib/tsc.js:36672:42)
    at getTypeFromTypeNode (/Users/boneskull/projects/boneskull/sync-monorepo-packages/node_modules/typescript/lib/tsc.js:37005:28)
    at getTypeFromTypeNode (/Users/boneskull/projects/boneskull/sync-monorepo-packages/node_modules/typescript/lib/tsc.js:36982:28)
    at getDeclaredTypeOfTypeAlias (/Users/boneskull/projects/boneskull/sync-monorepo-packages/node_modules/typescript/lib/tsc.js:33085:39)
    at tryGetDeclaredTypeOfSymbol (/Users/boneskull/projects/boneskull/sync-monorepo-packages/node_modules/typescript/lib/tsc.js:33216:24)

This _may or may not_ have anything to do with the fact I'm only generating declarations from .js files.

Bug Fix Available

Most helpful comment

Has this been fixed?
Im still getting the error testing against v3.8.0-dev.20191205

Here is my stack trace (the first one is react-styleguidist)

Trace
    at withContext (node_modules/typescript/lib/typescript.js:36361:25)
    at Object.typeToTypeNode (node_modules/typescript/lib/typescript.js:36316:28)
    at typeToString (node_modules/typescript/lib/typescript.js:36283:40)
    at Object.typeToString (node_modules/typescript/lib/typescript.js:33355:24)
    at Parser.getDocgenType (node_modules/react-docgen-typescript/lib/parser.js:284:43)
    at node_modules/react-docgen-typescript/lib/parser.js:331:29
    at Array.forEach (<anonymous>)
    at Parser.getPropsInfo (node_modules/react-docgen-typescript/lib/parser.js:309:27)
    at Parser.getComponentInfo (node_modules/react-docgen-typescript/lib/parser.js:129:30)
    at node_modules/react-docgen-typescript/lib/parser.js:702:27
    at Array.map (<anonymous>)
    at node_modules/react-docgen-typescript/lib/parser.js:701:14
    at Array.reduce (<anonymous>)
    at parseWithProgramProvider (node_modules/react-docgen-typescript/lib/parser.js:694:10)
    at parse (node_modules/react-docgen-typescript/lib/parser.js:71:20)
    at Object.module.exports (node_modules/react-styleguidist/lib/loaders/props-loader.js:41:12)
 FAIL  Failed to compile

[internal]
INTERNAL ERROR: Cannot read property 'valueDeclaration' of undefined
stack trace:
TypeError: Cannot read property 'valueDeclaration' of undefined
    at getBaseConstructorTypeOfClass (node_modules/typescript/lib/typescript.js:40026:40)
    at serializeAsClass (node_modules/typescript/lib/typescript.js:38036:42)
    at serializeSymbolWorker (node_modules/typescript/lib/typescript.js:37770:29)
    at serializeSymbol (node_modules/typescript/lib/typescript.js:37711:38)
    at node_modules/typescript/lib/typescript.js:37686:25
    at Map.forEach (<anonymous>)
    at visitSymbolTable (node_modules/typescript/lib/typescript.js:37685:33)
    at symbolTableToDeclarationStatements (node_modules/typescript/lib/typescript.js:37556:17)
    at node_modules/typescript/lib/typescript.js:36340:106
    at withContext (node_modules/typescript/lib/typescript.js:36362:37)
error Command failed with exit code 1.

All 26 comments

What's the content of foo.js ?

I was having this same issue with the new Typescript 3.7.2. The problem was the import below that's importing a const instead of a type.

_Component.jsx_

/** @typedef {import('app/utils/validations').INVALID_FILE} INVALID_FILE */

_app/utils/validations.js_

export const INVALID_FILE = 'INVALID_FILE';

I've also seen this issue when upgrading to TypeScript 3.7.2 (3.6.4 works fine)

I've narrowed it down so that I can only see the error (Cannot read property 'valueDeclaration' of undefined) when type annotations like the following are included:

  /**
   * @param {InstanceType<import('../../utils/templateUtils')['HasNoCostError']>} error
   */
  constructor(error) {

This is the syntax I currently use to annotate that a parameter is an instance of a class which is definied in a separate .js file. I also talk about this here: https://github.com/microsoft/TypeScript/issues/29485#issuecomment-518316889

I've created a small gist which should reproduce the problem: https://gist.github.com/lukehesluke/8ef8e7061c6533e208e055aedb4e31b1

Keen to see how this develops and happy to help if that's useful

I have the same issue. :(

I have no ts files in my repository, and only a couple that use jsdocs to import jsdoc typedefs and none of them use imports like @lukehesluke

Any advice for tracking down the offending file?

Building project '/home/name/projectName/tsconfig.json'...

/usr/local/lib/node_modules/typescript/lib/tsc.js:78566
                throw e;
                ^

TypeError: Cannot read property 'valueDeclaration' of undefined
    at getBaseConstructorTypeOfClass (/usr/local/lib/node_modules/typescript/lib/tsc.js:32851:40)
    at serializeAsClass (/usr/local/lib/node_modules/typescript/lib/tsc.js:31138:42)
    at serializeSymbolWorker (/usr/local/lib/node_modules/typescript/lib/tsc.js:30937:29)
    at serializeSymbol (/usr/local/lib/node_modules/typescript/lib/tsc.js:30894:38)
    at /usr/local/lib/node_modules/typescript/lib/tsc.js:30879:29
    at Map.forEach (<anonymous>)
    at visitSymbolTable (/usr/local/lib/node_modules/typescript/lib/tsc.js:30878:42)
    at symbolTableToDeclarationStatements (/usr/local/lib/node_modules/typescript/lib/tsc.js:30766:17)
    at /usr/local/lib/node_modules/typescript/lib/tsc.js:29639:106
    at withContext (/usr/local/lib/node_modules/typescript/lib/tsc.js:29659:37)

I tried to get the declaration files for the openlayers project, but failed with error

> shx rm -rf build/ol && shx mkdir -p build/ol && shx cp -rf src/ol build/ol/src && node tasks/serialize-workers && tsc --project config/tsconfig-build.json

/home/arszp/project/te/openlayers/node_modules/typescript/lib/tsc.js:78566
                throw e;
                ^

TypeError: Cannot read property 'valueDeclaration' of undefined
    at getExpandoSymbol (/home/arszp/project/te/openlayers/node_modules/typescript/lib/tsc.js:28787:31)
    at getTypeReferenceType (/home/arszp/project/te/openlayers/node_modules/typescript/lib/tsc.js:35269:22)
    at getTypeFromJSDocValueReference (/home/arszp/project/te/openlayers/node_modules/typescript/lib/tsc.js:35309:32)
    at getTypeReferenceType (/home/arszp/project/te/openlayers/node_modules/typescript/lib/tsc.js:35283:33)
    at getTypeFromJSDocValueReference (/home/arszp/project/te/openlayers/node_modules/typescript/lib/tsc.js:35309:32)
    at getTypeReferenceType (/home/arszp/project/te/openlayers/node_modules/typescript/lib/tsc.js:35283:33)
    at resolveImportSymbolType (/home/arszp/project/te/openlayers/node_modules/typescript/lib/tsc.js:36674:24)
    at getTypeFromImportTypeNode (/home/arszp/project/te/openlayers/node_modules/typescript/lib/tsc.js:36649:42)
    at getTypeFromTypeNode (/home/arszp/project/te/openlayers/node_modules/typescript/lib/tsc.js:36982:28)
    at checkImportType (/home/arszp/project/te/openlayers/node_modules/typescript/lib/tsc.js:49326:13)

an error is thrown at code similar to https://github.com/microsoft/TypeScript/issues/34926#issuecomment-550366377

Here's a sample code that'd reproduce this error:

// test.ts
declare namespace ಠ_ಠ.clutz.jspb {
  type AnyFieldType = boolean | number | string;
}
declare module 'goog:jspb.AnyFieldType' {
  import AnyFieldType = ಠ_ಠ.clutz.jspb.AnyFieldType;
  export default AnyFieldType;
}

And here's the runtime behavior when type checking this code:

// index.ts
import * as ts from 'typescript';

const program = ts.createProgram({
  rootNames: process.argv.slice(2),
  options: ts.getDefaultCompilerOptions(),
});

function visit(c: ts.Node): ts.ImportEqualsDeclaration|undefined {
  if (ts.isImportEqualsDeclaration(c)) {
    return c;
  }
  return c.forEachChild(visit);
}

let importEqDecl: ts.ImportEqualsDeclaration|undefined;
for (const root of program.getRootFileNames()) {
  const sf = program.getSourceFile(root)!;
  importEqDecl = visit(sf);
}

const tc = program.getTypeChecker();
const moduleRef = importEqDecl!.moduleReference;
const symbol = tc.getSymbolAtLocation(moduleRef)!;

console.error(ts.SymbolFlags[symbol.flags], symbol.name, symbol.valueDeclaration);
// Prints "TypeAlias AnyFieldType undefined"

To execute, run yarn tsc && node index.js test.ts

Typescript version: 3.5.3

@RyanCavanaugh from what I can tell, the contents of foo.js are irrelevant as long as it outputs some sort of declaration.

To @lepsch's comment, I'll check and see if I am also importing a value instead of a @typedef. Either way, this seems to be new behavior which was introduced somewhere after v3.7.0-dev.20191017 and on or before 3.8.0-dev.20191101. To be clear, in version v3.7.0-dev.20191017, I get an error about refusing to overwrite an existing .d.ts file.

Same is happening for us, the stacktrace only includes typescript files so i don't know what's causing it :(

I tried on 3.8.0-dev.20191105 and it was still the same

Same issue for me.

I've created a branch in my project which illustrates the problem with v3.7.2.

After cloning this branch, run npm install (or npm ci) and npm run build:declarations, which should dump the error. tsconfig.declarations.json is the config file used (extending tsconfig.base.json) by this script.

I know this is a far cry from a "minimal, reproducible example," but it's better than nothing. 😝

I just rolled back to the version 3.6.4: No errors with the same code.

Hey @RyanCavanaugh, you have critical production bug here in your compiler!

Is there a way to overcome this without downgrading to 3.6? Weird this issue hasn't got more responses as it seems to be pretty critical, my project just doesn't launch on 3.7.

I have created a minimal example to reproduce the issue in OpenLayers.

See https://gist.github.com/ahocevar/b3e58e7eac7750faa235ef66bdb559ab.

The problem appears to be related to the getTypeOfSymbol() call in the getTypeFromJSDocValueReference() function. In the above example, the return type of PointFoo's getType() method is not found, and getTypeOfSymbol() returns an errorType.

@ahocevar Thanks for that! -- I went through minimizing the "far cry" example above, which took almost the same time as getting through a far cry game, and got something manageable. The problem is when using an import in a jsdoc as you have in index.js.

I checked that it's fixed now, but it'll still be interesting to look at since it's slightly different.

Thanks for the fix @elibarzilay!

Has this been fixed?
Im still getting the error testing against v3.8.0-dev.20191205

Here is my stack trace (the first one is react-styleguidist)

Trace
    at withContext (node_modules/typescript/lib/typescript.js:36361:25)
    at Object.typeToTypeNode (node_modules/typescript/lib/typescript.js:36316:28)
    at typeToString (node_modules/typescript/lib/typescript.js:36283:40)
    at Object.typeToString (node_modules/typescript/lib/typescript.js:33355:24)
    at Parser.getDocgenType (node_modules/react-docgen-typescript/lib/parser.js:284:43)
    at node_modules/react-docgen-typescript/lib/parser.js:331:29
    at Array.forEach (<anonymous>)
    at Parser.getPropsInfo (node_modules/react-docgen-typescript/lib/parser.js:309:27)
    at Parser.getComponentInfo (node_modules/react-docgen-typescript/lib/parser.js:129:30)
    at node_modules/react-docgen-typescript/lib/parser.js:702:27
    at Array.map (<anonymous>)
    at node_modules/react-docgen-typescript/lib/parser.js:701:14
    at Array.reduce (<anonymous>)
    at parseWithProgramProvider (node_modules/react-docgen-typescript/lib/parser.js:694:10)
    at parse (node_modules/react-docgen-typescript/lib/parser.js:71:20)
    at Object.module.exports (node_modules/react-styleguidist/lib/loaders/props-loader.js:41:12)
 FAIL  Failed to compile

[internal]
INTERNAL ERROR: Cannot read property 'valueDeclaration' of undefined
stack trace:
TypeError: Cannot read property 'valueDeclaration' of undefined
    at getBaseConstructorTypeOfClass (node_modules/typescript/lib/typescript.js:40026:40)
    at serializeAsClass (node_modules/typescript/lib/typescript.js:38036:42)
    at serializeSymbolWorker (node_modules/typescript/lib/typescript.js:37770:29)
    at serializeSymbol (node_modules/typescript/lib/typescript.js:37711:38)
    at node_modules/typescript/lib/typescript.js:37686:25
    at Map.forEach (<anonymous>)
    at visitSymbolTable (node_modules/typescript/lib/typescript.js:37685:33)
    at symbolTableToDeclarationStatements (node_modules/typescript/lib/typescript.js:37556:17)
    at node_modules/typescript/lib/typescript.js:36340:106
    at withContext (node_modules/typescript/lib/typescript.js:36362:37)
error Command failed with exit code 1.

Similar to what @jasonwilliams said, can someone please help me understand the state of this bug?
Associated PR #35360 was merged into master and into release-3.7, and a later commit on that branch is tagged as v3.7.3.

Does that mean that this issue is fixed in v3.7.3?

@jasonwilliams, the stacktrace looks like it's in a different place, so probably a different bug...

@elibarzilay the top of the first stack trace leads into the bottom of the second one.
I needed to use console.trace() to go further because it ended at withContext()

I'm seeing the same stacktrace as @jasonwilliams

    TypeError: Cannot read property 'valueDeclaration' of undefined
        at getBaseConstructorTypeOfClass (/home/arian/git/main/frontend/node_modules/typescript/lib/typescript.js:40038:40)
        at serializeAsClass (/home/arian/git/main/frontend/node_modules/typescript/lib/typescript.js:38042:42)
        at serializeSymbolWorker (/home/arian/git/main/frontend/node_modules/typescript/lib/typescript.js:37776:29)
        at serializeSymbol (/home/arian/git/main/frontend/node_modules/typescript/lib/typescript.js:37717:38)
        at /home/arian/git/main/frontend/node_modules/typescript/lib/typescript.js:37699:29
        at Map.forEach (<anonymous>)
        at visitSymbolTable (/home/arian/git/main/frontend/node_modules/typescript/lib/typescript.js:37698:42)
        at symbolTableToDeclarationStatements (/home/arian/git/main/frontend/node_modules/typescript/lib/typescript.js:37562:17)
        at /home/arian/git/main/frontend/node_modules/typescript/lib/typescript.js:36347:106
        at withContext (/home/arian/git/main/frontend/node_modules/typescript/lib/typescript.js:36368:37)

Version: typescript 3.8.0-dev.20191207

See above -- this issue was in the neighborhood of resolveImportSymbolType down to getExpandoSymbol, and these new traces don't seem similar to that.

@elibarzilay right, so do you want me to make a new issue?
It may be a different trace but the error is still the same as this current issue

@jasonwilliams, yeah, with its own repro (hopefully more minimized than the one for this...)

Any update on this? I'm getting the error with the same stracktrace as @jasonwilliams, but unfortunately cannot narrow it down and make a repro as my project is pretty large and stack doesn't help at all.

Was this page helpful?
0 / 5 - 0 ratings