Skip to content

TypeError: Cannot read property 'valueDeclaration' of undefined #34869

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
boneskull opened this issue Nov 1, 2019 · 26 comments · Fixed by #35360
Closed

TypeError: Cannot read property 'valueDeclaration' of undefined #34869

boneskull opened this issue Nov 1, 2019 · 26 comments · Fixed by #35360
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@boneskull
Copy link
Contributor

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.

@RyanCavanaugh
Copy link
Member

What's the content of foo.js ?

@RyanCavanaugh RyanCavanaugh added the Needs More Info The issue still hasn't been fully clarified label Nov 4, 2019
@lepsch
Copy link

lepsch commented Nov 6, 2019

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';

@lukehesluke
Copy link

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: #29485 (comment)

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

@atotalnoob
Copy link

atotalnoob commented Nov 6, 2019

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)

@arszp10
Copy link

arszp10 commented Nov 7, 2019

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 #34926 (comment)

JakeChampion pushed a commit to Financial-Times/origami-build-service-v3 that referenced this issue Nov 7, 2019
@kyliau
Copy link
Contributor

kyliau commented Nov 7, 2019

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

@boneskull
Copy link
Contributor Author

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

@boneskull
Copy link
Contributor Author

boneskull commented Nov 7, 2019

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.

JakeChampion pushed a commit to Financial-Times/origami-build-service-v3 that referenced this issue Nov 8, 2019
@jasonwilliams
Copy link

jasonwilliams commented Nov 8, 2019

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

JakeChampion pushed a commit to Financial-Times/origami-build-service-v3 that referenced this issue Nov 8, 2019
JakeChampion pushed a commit to Financial-Times/origami-build-service-v3 that referenced this issue Nov 8, 2019
JakeChampion pushed a commit to Financial-Times/origami-build-service-v3 that referenced this issue Nov 8, 2019
JakeChampion pushed a commit to Financial-Times/origami-build-service-v3 that referenced this issue Nov 8, 2019
@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript and removed Needs More Info The issue still hasn't been fully clarified labels Nov 8, 2019
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 3.8.0 milestone Nov 8, 2019
@akopchinskiy
Copy link

Same issue for me.

boneskull added a commit to IBM/report-toolkit that referenced this issue Nov 13, 2019
@boneskull
Copy link
Contributor Author

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. 😝

@akopchinskiy
Copy link

akopchinskiy commented Nov 14, 2019

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!

@evenfrost
Copy link

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.

@ahocevar
Copy link

ahocevar commented Nov 26, 2019

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.

elibarzilay added a commit to elibarzilay/TypeScript that referenced this issue Nov 26, 2019
When using `{import('./b').FOO}` which is defined as a string literal,
`valueType` doesn't have a `symbol`.

This was exposed in 8223c07.

Fixes microsoft#34869.
@elibarzilay
Copy link
Contributor

@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.

@elibarzilay elibarzilay added the Fix Available A PR has been opened for this issue label Nov 26, 2019
@ahocevar
Copy link

Thanks for the fix @elibarzilay!

elibarzilay added a commit to elibarzilay/TypeScript that referenced this issue Nov 27, 2019
When using `{import('./b').FOO}` which is defined as a string literal,
`valueType` doesn't have a `symbol`.  Leave it for the fallback value
for now.

This was exposed in 8223c07.

Fixes microsoft#34869.
elibarzilay added a commit that referenced this issue Nov 27, 2019
When using `{import('./b').FOO}` which is defined as a string literal,
`valueType` doesn't have a `symbol`.  Leave it for the fallback value
for now.

This was exposed in 8223c07.

Fixes #34869.
elibarzilay added a commit that referenced this issue Nov 28, 2019
When using `{import('./b').FOO}` which is defined as a string literal,
`valueType` doesn't have a `symbol`.  Leave it for the fallback value
for now.

This was exposed in 8223c07.

Fixes #34869.
@jasonwilliams
Copy link

jasonwilliams commented Dec 5, 2019

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.

@BernsteinA
Copy link

BernsteinA commented Dec 5, 2019

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?

@elibarzilay
Copy link
Contributor

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

@jasonwilliams
Copy link

jasonwilliams commented Dec 6, 2019

@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()

@arian
Copy link

arian commented Dec 9, 2019

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

@elibarzilay
Copy link
Contributor

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

@jasonwilliams
Copy link

jasonwilliams commented Dec 10, 2019

@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

@elibarzilay
Copy link
Contributor

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

@evenfrost
Copy link

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.

@jasonwilliams
Copy link

This has continued over at #35801

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.