TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

tsc crash with "TypeError: Cannot read properties of undefined (reading 'flags')"

Open mirek opened this issue 1 year ago • 5 comments

🔎 Search Terms

none

🕗 Version & Regression Information

  • This is a crash

⏯ Playground Link

No response

💻 Code

// Too large / internal

🙁 Actual behavior

tsc crashes

🙂 Expected behavior

tsc should not crash

Additional information about the issue

 /home/runner/work/my-project/node_modules/.pnpm/[email protected]/node_modules/typescript/lib/tsc.js:118617
      throw e;
      ^

TypeError: Cannot read properties of undefined (reading 'flags')
    at getCheckFlags (/home/runner/work/my-project/node_modules/.pnpm/[email protected]/node_modules/typescript/lib/tsc.js:15928:17)
    at getTypeOfSymbol (/home/runner/work/my-project/node_modules/.pnpm/[email protected]/node_modules/typescript/lib/tsc.js:53259:24)
    at getParameterCount (/home/runner/work/my-project/node_modules/.pnpm/[email protected]/node_modules/typescript/lib/tsc.js:73669:24)
    at combineUnionParameters (/home/runner/work/my-project/node_modules/.pnpm/[email protected]/node_modules/typescript/lib/tsc.js:54395:23)
    at combineSignaturesOfUnionMembers (/home/runner/work/my-project/node_modules/.pnpm/[email protected]/node_modules/typescript/lib/tsc.js:54443:20)
    at /home/runner/work/my-project/node_modules/.pnpm/[email protected]/node_modules/typescript/lib/tsc.js:54359:204
    at map (/home/runner/work/my-project/node_modules/.pnpm/[email protected]/node_modules/typescript/lib/tsc.js:204:19)
    at getUnionSignatures (/home/runner/work/my-project/node_modules/.pnpm/[email protected]/node_modules/typescript/lib/tsc.js:54359:182)
    at resolveUnionTypeMembers (/home/runner/work/my-project/node_modules/.pnpm/[email protected]/node_modules/typescript/lib/tsc.js:54482:28)
    at resolveStructuredTypeMembers (/home/runner/work/my-project/node_modules/.pnpm/[email protected]/node_modules/typescript/lib/tsc.js:54946:9)

Node.js v20.9.0
 ELIFECYCLE  Command failed with exit code 1.

mirek avatar Apr 30 '24 12:04 mirek

It's close to impossible to fix an issue like this without a repro case. https://antfu.me/posts/why-reproductions-are-required

Andarist avatar Apr 30 '24 14:04 Andarist

Fair enough, I thought stacktrace gives enough hints. I'll try to narrow it down.

mirek avatar Apr 30 '24 17:04 mirek

This should be a pretty easy one to isolate based on seeing which nodes/types/symbols are in the preceding stack frames.

RyanCavanaugh avatar Apr 30 '24 19:04 RyanCavanaugh

Is there a way to dump it easily? Still going through picking changes from refactor pull request that introduced it (it's quarter of a million LoC project).

mirek avatar May 02 '24 06:05 mirek

I'd setup a breakpoint there, like this:

 function getCheckFlags(symbol) {
+    if (!symbol) { debugger; }
     return symbol.flags & 33554432 ? symbol.links.checkFlags : 0;
 }

And when u get inevitably paused there you could look up the call stack, look through objects used there etc and try to narrow it down based on that information.

Andarist avatar May 02 '24 07:05 Andarist

I encountered the same error in my project, and I can provide the code to reproduce it. When I compile the following code using npx tsc, it throws TypeError: Cannot read properties of undefined (reading 'flags').

type T1 = "A" | "B"

type T2 = {
    "C": [string]
    "D": [number]
}

const commands: {
    [K in T1 | keyof T2]: (...args: K extends keyof T2 ? T2[K] : []) => unknown
} = {
    async A() { },
    async B() { },
    async C() { },
    async D() { },
}

for (const [key, fn] of Object.entries(commands)) {
    // @ts-expect-error
    fn(...args)
}

const x: number = "a"  // the ts server is dead and no compilation error is shown

Playground link: https://www.typescriptlang.org/play/?ts=5.5.0-dev.20240504#code/C4TwDgpgBAKgjFAvFARAQRVAPqgQigKANElgCYkoBvAqO1AYRQC4oBtAZ2ACcBLAOwDmAXVr0UAERbt+AVwC2AIwjdRAXyIBjAPb8uUeQEMwrGvXYBpKANgIcAawghtAM3LDWACgB0vw90EOVisIAA9gCH4AEw4oR2c3GAoAfnI2C2EoVjZhAEokAD4oWX57fm0Ad34CNUozekMOEH5NKDRPfKooNQAaMTpG5tbcDupuvvNBlqgGUa7e-qgp1ok58ZqiF21uKE8dPWB2eJ6oF35M1ygAeUUAKwhNYG9Inl4IDk8jMFzOxYB6P5QAACwA4AFowpBHhDuNxtoszj4-AEOLkNgR9vpQqw5EoVJQUIZMFAAVBgAALaCgqAcFQAN3xvFiUQghiiS2iUHKUB08jAvAANoZgLxdFAVHCdkyaeTKvwgA

yy0931 avatar May 05 '24 01:05 yy0931

Thanks! This can be slimmed down further to:

type T1 = "A" | "B";

type T2 = {
  C: [string];
  D: [number];
};

declare const map: {
  [K in T1 | keyof T2]: (...args: K extends keyof T2 ? T2[K] : []) => unknown;
};

for (const [key, fn] of Object.entries(map)) {
  fn(...args);
}

Andarist avatar May 05 '24 06:05 Andarist

@mirek could you check if this build fixes your issue?

Andarist avatar May 05 '24 17:05 Andarist

@Andarist yes, this build fixes issue on my side.

There is no crash.

I can see some type errors, some look completely unrelated, some look closer to the changes introduced in the PR that was triggering the crash.

I'll assume those are irrelevant unless somebody says otherwise then I can go through them. They look like normal, good/genuine tsc errors.

mirek avatar May 05 '24 19:05 mirek

@mirek you can check this new build

Andarist avatar May 06 '24 22:05 Andarist

@Andarist looks good on my side, there is no crash.

mirek avatar May 09 '24 07:05 mirek

Hi @Andarist and @RyanCavanaugh! Apologies for the tags on a months-old thread, but my team is running into this same issue on our project. We use Vue, so we're running tsc indirectly via vue-tsc. I've also opened an issue with them to see if they have any ideas: https://github.com/vuejs/language-tools/issues/4578.

I can confirm that the build above fixes it for us, so I was curious if there were any updates on this? We're totally blocked from upgrading vue-tsc and therefore typescript for now. Not a huge deal for us, but just thought I'd check in.

Thank you!!

pdhoopr avatar Jul 11 '24 18:07 pdhoopr