Skip to content

Cannot read property 'flags' of undefined(3.2.x) #28935

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
xiaocaibird opened this issue Dec 10, 2018 · 9 comments
Closed

Cannot read property 'flags' of undefined(3.2.x) #28935

xiaocaibird opened this issue Dec 10, 2018 · 9 comments
Assignees
Labels
Bug A bug in TypeScript Crash For flagging bugs which are compiler or service crashes or unclean exits, rather than bad output Domain: Control Flow The issue relates to control flow analysis

Comments

@xiaocaibird
Copy link

TypeScript Version: 3.2.x

Search Terms: Cannot read property 'flags' of undefined

Code

First of all, I am sorry. This is an internal project of our company, I can't provide a git url.
And because of the large amount of code, I still can't find the wrong code snippet.
Our project is still working in the previous version(3.1.x).
I found this problem in 3.2.1,and I found some related issues at the time.
I was hoping it could be fixed together, but no.

Expected behavior: Compile

Actual behavior:
TypeError: Cannot read property 'flags' of undefined
at isFreshLiteralType (/project/news-manage/node_modules/typescript/lib/tsc.js:33326:28)
at isTypeRelatedTo (/project/news-manage/node_modules/typescript/lib/tsc.js:34425:17)
at isTypeComparableTo (/project/news-manage/node_modules/typescript/lib/tsc.js:33930:20)
at /project/news-manage/node_modules/typescript/lib/tsc.js:37851:102
at Object.filter (/project/news-manage/node_modules/typescript/lib/tsc.js:340:31)
at filterType (/project/news-manage/node_modules/typescript/lib/tsc.js:37422:35)
at narrowTypeByDiscriminant (/project/news-manage/node_modules/typescript/lib/tsc.js:37851:63)
at narrowTypeByTruthiness (/project/news-manage/node_modules/typescript/lib/tsc.js:37858:28)
at narrowType (/project/news-manage/node_modules/typescript/lib/tsc.js:38161:32)
at getTypeAtFlowCondition (/project/news-manage/node_modules/typescript/lib/tsc.js:37730:36)

Playground Link:

Related Issues:

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Dec 10, 2018

narrowTypeByDiscriminant is the call that has two type assertions

https://github.com/Microsoft/TypeScript/blob/c2898db9dd58c9b0681d6131ed6076d71d89e6b5/src/compiler/checker.ts#L15656

return propType === narrowedPropType ? type : filterType(type, t => isTypeComparableTo(getTypeOfPropertyOfType(t, propName)!, narrowedPropType!));

@xiaocaibird
Copy link
Author

@DanielRosenwasser
Thanks.
But, how can I solve this problem.

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Dec 10, 2018

That comment was meant more for anyone who ends up assigned to this issue 😃

@DanielRosenwasser DanielRosenwasser added Bug A bug in TypeScript Domain: Control Flow The issue relates to control flow analysis labels Dec 10, 2018
@DanielRosenwasser DanielRosenwasser added the Crash For flagging bugs which are compiler or service crashes or unclean exits, rather than bad output label Dec 10, 2018
@DanielRosenwasser DanielRosenwasser added this to the TypeScript 3.3 milestone Dec 10, 2018
@ahejlsberg
Copy link
Member

Here's a repro that crashes with a similar call stack:

type Foo = { tag: true, x: number } | { tag: false, y: number } | { [x: string]: string };

function test(foo: Foo) {
    if (!foo.tag) {
        foo;
    }
}

The issue is that we assume that each constituent has a discriminant property when it is possible that some don't, but rather have a string index signature.

@ahejlsberg
Copy link
Member

@xiaocaibird Just merged what I think is a fix to your issue. Try pulling the next nightly build and let us know what you find.

@zedfight
Copy link

+1 same issue here

@DanielRosenwasser
Copy link
Member

Have you tried the nightly build? npm install typescript@next

@zedfight
Copy link

Have you tried the nightly build? npm install typescript@next

Fix. Thanks!!

@DanielRosenwasser
Copy link
Member

Great! Glad we could fix it quickly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Crash For flagging bugs which are compiler or service crashes or unclean exits, rather than bad output Domain: Control Flow The issue relates to control flow analysis
Projects
None yet
Development

No branches or pull requests

4 participants