microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
101.04k stars 12.49k forks source link

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

Closed bluelovers closed 5 years ago

bluelovers commented 5 years ago

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 commented 5 years ago

@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 commented 5 years ago

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 commented 5 years ago

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

patrickmichalina commented 5 years ago

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

PopGoesTheWza commented 5 years ago

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 commented 5 years ago

Would you please release the fix as 3.4.x?

RyanCavanaugh commented 5 years ago

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

andrewbranch commented 5 years ago

Done: https://github.com/Microsoft/TypeScript/pull/30892

Jack-Works commented 5 years ago

Happened on (x: readonly T[])

andrewbranch commented 5 years ago

Re-closing as this has been released