Typescript: TypeError: Cannot read property 'instantiations' of undefined when use `as const`

Created on 30 Mar 2019  路  10Comments  路  Source: microsoft/TypeScript


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:

Bug

Most helpful comment

All 10 comments

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

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.

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

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

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]) {
}

Would you please release the fix as 3.4.x?

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

Happened on (x: readonly T[])

Re-closing as this has been released

Was this page helpful?
0 / 5 - 0 ratings