Skip to content

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

Description

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:

Activity

  1. j-oliveras commented on Mar 30, 2019

    @j-oliveras
    Contributor

    Anders Hejlsberg (@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.

  2. ajafff commented on Mar 30, 2019

    @ajafff
    Contributor

    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.

  3. iFwu commented on Apr 1, 2019

    @iFwu

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

  4. patrickmichalina commented on Apr 3, 2019

    @patrickmichalina

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

  5. PopGoesTheWza commented on Apr 3, 2019

    @PopGoesTheWza

    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]) {
    }
  6. ddrozdov commented on Apr 12, 2019

    @ddrozdov

    Would you please release the fix as 3.4.x?

  7. RyanCavanaugh commented on Apr 12, 2019

    @RyanCavanaugh
    Member

    Andrew Branch (@andrewbranch) please port the fix PR to the release-3.4 branch. Thanks!

  8. andrewbranch commented on Apr 12, 2019

    @andrewbranch
    Member

    Done: #30892

  9. Jack-Works commented on Apr 17, 2019

    @Jack-Works
    Contributor

    Happened on (x: readonly T[])

  10. andrewbranch commented on May 17, 2019

    @andrewbranch
    Member

    Re-closing as this has been released

  11. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions