Skip to content

Crash with Cannot read property 'flags' of undefined (union related?) #29884

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

Closed
haltcase opened this issue Feb 12, 2019 · 6 comments
Closed

Crash with Cannot read property 'flags' of undefined (union related?) #29884

haltcase opened this issue Feb 12, 2019 · 6 comments
Assignees

Comments

@haltcase
Copy link

haltcase commented Feb 12, 2019

TypeScript Version: 3.3.3 / 3.4.0-dev.20190209

Search Terms:
crash union intersection cannot read property flags undefined

Code

Unfortunately this came up during a big refactor so I don't have a small repro case, but the next branch of trilogy reproduces it here:

  1. git clone --single-branch --branch next https://github.com/citycide/trilogy.git
  2. cd trilogy
  3. npm install
  4. ./node_modules/.bin/tsc

I reproduced on both Windows 10 & Ubuntu, plus my Travis CI suite. Interestingly VS Code's TS server doesn't show any errors at all and keeps type checking like normal.

Based on the call stack we can probably assume it's due to a union or intersection type somewhere but I don't know how to trace it back effectively.

Expected behavior:

Compile without crashing.

Actual behavior:

> .\node_modules\.bin\tsc
E:\dev\trilogy\node_modules\typescript\lib\tsc.js:71784
                throw e;
                ^

TypeError: Cannot read property 'flags' of undefined
    at getRegularTypeOfLiteralType (E:\dev\trilogy\node_modules\typescript\lib\tsc.js:33693:25)
    at addTypesToIntersection (E:\dev\trilogy\node_modules\typescript\lib\tsc.js:32901:69)
    at getIntersectionType (E:\dev\trilogy\node_modules\typescript\lib\tsc.js:32973:28)
    at combineUnionParameters (E:\dev\trilogy\node_modules\typescript\lib\tsc.js:30815:38)
    at combineSignaturesOfUnionMembers (E:\dev\trilogy\node_modules\typescript\lib\tsc.js:30833:26)
    at E:\dev\trilogy\node_modules\typescript\lib\tsc.js:30782:181
    at Object.map (E:\dev\trilogy\node_modules\typescript\lib\tsc.js:426:29)
    at _loop_5 (E:\dev\trilogy\node_modules\typescript\lib\tsc.js:30782:144)
    at getUnionSignatures (E:\dev\trilogy\node_modules\typescript\lib\tsc.js:30790:35)
    at resolveUnionTypeMembers (E:\dev\trilogy\node_modules\typescript\lib\tsc.js:30857:34)

Playground Link:

Related Issues:
This error message is pretty common so there are a lot of issues referencing it, however they vary pretty wildly in the call stack: #28935, #27345, #25387, #25264, #15456, #19142, #2127

@haltcase haltcase mentioned this issue Feb 19, 2019
18 tasks
@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Feb 19, 2019
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 3.4.0 milestone Feb 19, 2019
@RyanCavanaugh
Copy link
Member

The current commit of next is 04404fe

@VoronFX
Copy link

VoronFX commented Feb 20, 2019

Here is minimal setup that reproduces the issue:

bug.ts

export const KeysOf = <T extends Record<string, any>>(object: T) =>
    Object.keys(object) as (keyof T extends string ? Array<keyof T> : never);

export const mapObject = <
    T extends Record<keyof T, any>,
    P2,
    P = T extends Record<keyof T, infer P> ? P : any
>(
    input: T,
    mapper: (prop: P, key: keyof T) => P2
) => {
    // tslint:disable-next-line:no-object-literal-type-assertion
    const newObject = {} as Record<keyof T, P2>;
    KeysOf(input).forEach(key => {
        newObject[key] = mapper(input[key], key);
    });
    return newObject;
};

export type NewLessType<
    C extends new (...args: A) => T,
    A extends any[] = C extends new (...args: infer R) => any ? R : never,
    T = C extends new (...args: A) => infer R ? R : never
> = (...args: A) => T;

type x = new (x: Pick<{ a: number }, 'a'>) => Pick<{ a: number }, 'a'>;
type x2 = new (z: Pick<{ b: number }, 'b'>) => Pick<{ b: number }, 'b'>;
type x3 = new (z: Pick<{ c: number }, 'c'>) => Pick<{ c: number }, 'c'>;

type x4 = NewLessType<x> | NewLessType<x2> | NewLessType<x3>;

const tt = {
    tick: null! as NewLessType<x>,
    tick2: null! as NewLessType<x2>,
    tick3: null! as NewLessType<x3>
};

const a = mapObject(
    {
        tick: null! as NewLessType<x>,
        tick2: null! as NewLessType<x2>,
        tick3: null! as NewLessType<x3>
    },
    x => (...args: any[]) => {
        x(...args);
    }
);

commands:

npm i typescript
node .\node_modules\typescript\bin\tsc bug.ts

output:

C:\Users\Voron\Source\Repos\poc\example\node_modules\typescript\lib\tsc.js:71491
                throw e;
                ^

TypeError: Cannot read property 'flags' of undefined
    at getRegularTypeOfLiteralType (C:\Users\Voron\Source\Repos\poc\example\node_modules\typescript\lib\tsc.js:33580:25)
    at addTypesToIntersection (C:\Users\Voron\Source\Repos\poc\example\node_modules\typescript\lib\tsc.js:32787:69)
    at getIntersectionType (C:\Users\Voron\Source\Repos\poc\example\node_modules\typescript\lib\tsc.js:32859:28)
    at combineUnionParameters (C:\Users\Voron\Source\Repos\poc\example\node_modules\typescript\lib\tsc.js:30715:38)
    at combineSignaturesOfUnionMembers (C:\Users\Voron\Source\Repos\poc\example\node_modules\typescript\lib\tsc.js:30733:26)
    at C:\Users\Voron\Source\Repos\poc\example\node_modules\typescript\lib\tsc.js:30682:181
    at Object.map (C:\Users\Voron\Source\Repos\poc\example\node_modules\typescript\lib\tsc.js:378:29)
    at _loop_5 (C:\Users\Voron\Source\Repos\poc\example\node_modules\typescript\lib\tsc.js:30682:144)
    at getUnionSignatures (C:\Users\Voron\Source\Repos\poc\example\node_modules\typescript\lib\tsc.js:30690:35)
    at resolveUnionTypeMembers (C:\Users\Voron\Source\Repos\poc\example\node_modules\typescript\lib\tsc.js:30757:34)

PS: Sorry for an ugly code, had no time for digging deeper. Hope my sample will help.
Here is full files.
bug.zip

@XuluWarrior
Copy link

We get a very similar but different call stack. I've checked the various versions from 3.1.6 to 3.4.0-dev.20190227 and it has been consistently broken for us since 3.2.

The call stack when using 3.3.3333 is

TypeError: Cannot read property 'flags' of undefined
    at createSymbolWithType (/Users/a/WebstormProjects/mercury-orig/node_modules/typescript/lib/tsc.js:36391:46)
    at getUnionSignatures (/Users/a/WebstormProjects/mercury-orig/node_modules/typescript/lib/tsc.js:30668:53)
    at resolveUnionTypeMembers (/Users/a/WebstormProjects/mercury-orig/node_modules/typescript/lib/tsc.js:30761:34)
    at resolveStructuredTypeMembers (/Users/a/WebstormProjects/mercury-orig/node_modules/typescript/lib/tsc.js:31051:21)
    at getSignaturesOfStructuredType (/Users/a/WebstormProjects/mercury-orig/node_modules/typescript/lib/tsc.js:31481:32)
    at getSignaturesOfType (/Users/a/WebstormProjects/mercury-orig/node_modules/typescript/lib/tsc.js:31487:20)
    at resolveCallExpression (/Users/a/WebstormProjects/mercury-orig/node_modules/typescript/lib/tsc.js:41699:34)
    at resolveSignature (/Users/a/WebstormProjects/mercury-orig/node_modules/typescript/lib/tsc.js:41963:28)
    at getResolvedSignature (/Users/a/WebstormProjects/mercury-orig/node_modules/typescript/lib/tsc.js:41984:26)
    at checkCallExpression (/Users/a/WebstormProjects/mercury-orig/node_modules/typescript/lib/tsc.js:42053:29)

@sheetalkamat sheetalkamat added Fixed A PR has been merged for this issue and removed Bug A bug in TypeScript Fixed A PR has been merged for this issue labels Mar 13, 2019
@sheetalkamat
Copy link
Member

This seems to be fixed with typescript@next

@XuluWarrior
Copy link

Unfortunately trying typescript@next (3.4.0-dev.20190313) didn't fix our compilation problems. I'll raise a new issue when I have time to provide some useful context.

@XuluWarrior
Copy link

I haven't managed to reduce our issue to a minimum reproduction. But I have created a letter-box fix (#30596) which works for us.

What can I do to assist turning this into a complete PR/fix?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants