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

TypeError: Cannot read property 'instantiations' of undefined when use as const #30664

Closed
bluelovers opened this issue Mar 30, 2019 · 10 comments
Closed
Assignees
Labels
Bug A bug in TypeScript

Comments

@bluelovers
Copy link
Contributor

TypeScript Version: 3.4.0-dev.201xxxxx

Search Terms:

Code

import * as ts from "typescript";

const source = `([
\t'dependencies',
\t'devDependencies',
\t'peerDependencies',
\t'optionalDependencies',
\t'resolutions',
] as const)
\t.forEach(k => {
\t\tconsole.log(k);
\t})
;`;

let result = ts.transpileModule(source, {
	compilerOptions: { module: ts.ModuleKind.CommonJS }
});

console.log(JSON.stringify(result));

Expected behavior:

Actual behavior:

C:\Users\User\AppData\Roaming\npm\node_modules\typescript\lib\typescript.js:89093
                throw e;
                ^

TypeError: Cannot read property 'instantiations' of undefined
    at createTypeReference (C:\Users\User\AppData\Roaming\npm\node_modules\typescript\lib\typescript.js:39071:31)
    at createTypeFromGenericGlobalType (C:\Users\User\AppData\Roaming\npm\node_modules\typescript\lib\typescript.js:39495:61)
    at createArrayType (C:\Users\User\AppData\Roaming\npm\node_modules\typescript\lib\typescript.js:39513:20)
    at getBaseTypes (C:\Users\User\AppData\Roaming\npm\node_modules\typescript\lib\typescript.js:36591:47)
    at resolveObjectTypeMembers (C:\Users\User\AppData\Roaming\npm\node_modules\typescript\lib\typescript.js:37283:29)
    at resolveTypeReferenceMembers (C:\Users\User\AppData\Roaming\npm\node_modules\typescript\lib\typescript.js:37314:13)
    at resolveStructuredTypeMembers (C:\Users\User\AppData\Roaming\npm\node_modules\typescript\lib\typescript.js:37884:25)
    at getPropertyOfType (C:\Users\User\AppData\Roaming\npm\node_modules\typescript\lib\typescript.js:38405:32)
    at checkPropertyAccessExpressionOrQualifiedName (C:\Users\User\AppData\Roaming\npm\node_modules\typescript\lib\typescript.js:49210:24)
    at checkPropertyAccessExpression (C:\Users\User\AppData\Roaming\npm\node_modules\typescript\lib\typescript.js:49193:20)

Playground Link:

Related Issues:

@j-oliveras
Copy link
Contributor

@ahejlsberg I investigated this: inside createArrayType function on checker.ts, the variable globalReadonlyArrayType is undefined as if its initialization cannot find the ReadonlyArray declaration.

I cannot link to the lines because the file is too big for GitHub.

@ajafff
Copy link
Contributor

ajafff commented Mar 30, 2019

initialization cannot find the ReadonlyArray declaration.

That's kind of expected since there is no lib.xxx.d.ts used by transpileModule. Defining ReadonlyArray<T> in the transpiled file should work around the crash.

This probably needs an additional condition to check if the global type is actually available. Though I don't know what a reasonable fallback would be.
Note that this might happen for globalArrayType and friends.

@iFwu
Copy link

iFwu commented Apr 1, 2019

I have this issue when using parcel to compile typescript files that use as const syntax.

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Apr 1, 2019
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 3.5.0 milestone Apr 1, 2019
@patrickmichalina
Copy link

Seeing the same failure when applying readonly to tuples like readonly [any, any]

@PopGoesTheWza
Copy link

I experience the same issue with typescript 3.4.1 while running ts.transpileModuleon the sample code for Improved support for read-only arrays and tuples

Error test code can be reduced to this:

function f2(ma: string[], ra: readonly string[], mt: [string, string], rt: readonly [string, string]) {
}

@ddrozdov
Copy link

Would you please release the fix as 3.4.x?

@RyanCavanaugh RyanCavanaugh reopened this Apr 12, 2019
@RyanCavanaugh
Copy link
Member

@andrewbranch please port the fix PR to the release-3.4 branch. Thanks!

@andrewbranch
Copy link
Member

Done: #30892

@Jack-Works
Copy link
Contributor

Happened on (x: readonly T[])

@andrewbranch
Copy link
Member

Re-closing as this has been released

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

10 participants