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

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

Open
mirek opened this issue Apr 30, 2024 · 11 comments Β· May be fixed by #58440
Open

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

mirek opened this issue Apr 30, 2024 · 11 comments Β· May be fixed by #58440
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@mirek
Copy link

mirek commented Apr 30, 2024

πŸ”Ž 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/typescript@5.4.3/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/typescript@5.4.3/node_modules/typescript/lib/tsc.js:15928:17)
    at getTypeOfSymbol (/home/runner/work/my-project/node_modules/.pnpm/typescript@5.4.3/node_modules/typescript/lib/tsc.js:53259:24)
    at getParameterCount (/home/runner/work/my-project/node_modules/.pnpm/typescript@5.4.3/node_modules/typescript/lib/tsc.js:73669:24)
    at combineUnionParameters (/home/runner/work/my-project/node_modules/.pnpm/typescript@5.4.3/node_modules/typescript/lib/tsc.js:54395:23)
    at combineSignaturesOfUnionMembers (/home/runner/work/my-project/node_modules/.pnpm/typescript@5.4.3/node_modules/typescript/lib/tsc.js:54443:20)
    at /home/runner/work/my-project/node_modules/.pnpm/typescript@5.4.3/node_modules/typescript/lib/tsc.js:54359:204
    at map (/home/runner/work/my-project/node_modules/.pnpm/typescript@5.4.3/node_modules/typescript/lib/tsc.js:204:19)
    at getUnionSignatures (/home/runner/work/my-project/node_modules/.pnpm/typescript@5.4.3/node_modules/typescript/lib/tsc.js:54359:182)
    at resolveUnionTypeMembers (/home/runner/work/my-project/node_modules/.pnpm/typescript@5.4.3/node_modules/typescript/lib/tsc.js:54482:28)
    at resolveStructuredTypeMembers (/home/runner/work/my-project/node_modules/.pnpm/typescript@5.4.3/node_modules/typescript/lib/tsc.js:54946:9)

Node.js v20.9.0
 ELIFECYCLE  Command failed with exit code 1.
@Andarist
Copy link
Contributor

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

@mirek
Copy link
Author

mirek commented Apr 30, 2024

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

@RyanCavanaugh RyanCavanaugh added the Needs More Info The issue still hasn't been fully clarified label Apr 30, 2024
@RyanCavanaugh
Copy link
Member

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

@mirek
Copy link
Author

mirek commented May 2, 2024

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

@Andarist
Copy link
Contributor

Andarist commented May 2, 2024

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.

@yy0931
Copy link

yy0931 commented May 5, 2024

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

@Andarist
Copy link
Contributor

Andarist commented May 5, 2024

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
Copy link
Contributor

Andarist commented May 5, 2024

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

@mirek
Copy link
Author

mirek commented May 5, 2024

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

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Help Wanted You can do this and removed Needs More Info The issue still hasn't been fully clarified labels May 6, 2024
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone May 6, 2024
@Andarist
Copy link
Contributor

Andarist commented May 6, 2024

@mirek you can check this new build

@mirek
Copy link
Author

mirek commented May 9, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
4 participants