Skip to content

Can't affect unknown to class with private field: Cannot read property 'flags' of undefined #37014

Description

TypeScript Version: 3.8.2

Search Terms: 'flags' of undefined

Code

export class Class {
  #field: any
}

const task: Class = {} as unknown

Expected behavior:

To give the compile error: Type 'unknown' is not assignable to type 'Class'.

Actual behavior:

Crashes with :

/Users/valentin/Zenika/Projects/cuillere/node_modules/typescript/lib/tsc.js:80993
                throw e;
                ^

TypeError: Cannot read property 'flags' of undefined
    at reportUnmatchedProperty (/Users/valentin/Zenika/Projects/cuillere/node_modules/typescript/lib/tsc.js:40618:38)
    at propertiesRelatedTo (/Users/valentin/Zenika/Projects/cuillere/node_modules/typescript/lib/tsc.js:40663:25)
    at structuredTypeRelatedTo (/Users/valentin/Zenika/Projects/cuillere/node_modules/typescript/lib/tsc.js:40349:34)
    at recursiveTypeRelatedTo (/Users/valentin/Zenika/Projects/cuillere/node_modules/typescript/lib/tsc.js:40096:53)
    at isRelatedTo (/Users/valentin/Zenika/Projects/cuillere/node_modules/typescript/lib/tsc.js:39777:38)
    at checkTypeRelatedTo (/Users/valentin/Zenika/Projects/cuillere/node_modules/typescript/lib/tsc.js:39487:26)
    at checkTypeRelatedToAndOptionallyElaborate (/Users/valentin/Zenika/Projects/cuillere/node_modules/typescript/lib/tsc.js:38806:24)
    at checkTypeAssignableToAndOptionallyElaborate (/Users/valentin/Zenika/Projects/cuillere/node_modules/typescript/lib/tsc.js:38800:20)
    at checkVariableLikeDeclaration (/Users/valentin/Zenika/Projects/cuillere/node_modules/typescript/lib/tsc.js:52072:25)
    at checkVariableDeclaration (/Users/valentin/Zenika/Projects/cuillere/node_modules/typescript/lib/tsc.js:52136:20)

Playground Link: I didn't found a way to reproduce this behaviour in the playground.

Related Issues: #36871 and #36677

Activity

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