Skip to content

Crash checking protected method accessibility: Cannot read property 'target' of undefined #47965

@alasher

Description

@alasher

Bug Report

I'm getting a compiler crash. It looks like it's trying to verify the accessibility of a class's protected method. I'm accessing it from a method in another class, and am accessing it via the lexical scope. I think this is related to the fact that I've explicitly defined the type of this to be T, if I remove this it doesn't crash anymore. Surprisingly, if the member is either private or public the result is correct and it no longer crashes. Please see the code sample below.

🔎 Search Terms

I searched for issues tagged as "Crash", of which none seemed to match this issue. I also looked around using variants of is:issue is:open "Cannot read property" "target" "of undefined", and I couldn't find a similar issue.

🕗 Version & Regression Information

  • This is a crash visible on nightly, I tested today on Version 4.7.0-dev.20220218.

⏯ Playground Link

As requested, here is a playground link with relevant code, but it's not very useful as the website becomes unresponsive due to the tsc crash.

💻 Code

export class Message {
    protected secret(): void { }
}

export class MessageWrapper {
    message: Message;
    wrap<T>() {
        let m = this.message;
        let f = function(this: T) {
            m.secret();
        }
    }
}

🙁 Actual behavior

This input causes tsc to crash with the following error:

/c/work/tsc_crash$ npx tsc -v
Version 4.7.0-dev.20220218
/c/work/tsc_crash$ npx tsc ./message.ts
C:\work\tsc_crash\node_modules\typescript\lib\tsc.js:95457
                throw e;
                ^

TypeError: Cannot read property 'target' of undefined
    at checkPropertyAccessibilityAtLocation (C:\work\tsc_crash\node_modules\typescript\lib\tsc.js:60898:114)
    at checkPropertyAccessibility (C:\work\tsc_crash\node_modules\typescript\lib\tsc.js:60839:20)
    at checkPropertyAccessExpressionOrQualifiedName (C:\work\tsc_crash\node_modules\typescript\lib\tsc.js:61159:17)
    at checkPropertyAccessExpression (C:\work\tsc_crash\node_modules\typescript\lib\tsc.js:60967:17)
    at checkExpressionWorker (C:\work\tsc_crash\node_modules\typescript\lib\tsc.js:65661:28)
    at checkExpression (C:\work\tsc_crash\node_modules\typescript\lib\tsc.js:65582:38)
    at resolveCallExpression (C:\work\tsc_crash\node_modules\typescript\lib\tsc.js:62615:28)
    at resolveSignature (C:\work\tsc_crash\node_modules\typescript\lib\tsc.js:62988:28)
    at getResolvedSignature (C:\work\tsc_crash\node_modules\typescript\lib\tsc.js:63008:26)
    at checkCallExpression (C:\work\tsc_crash\node_modules\typescript\lib\tsc.js:63117:29)

🙂 Expected behavior

Because this is a protected method, it shouldn't crash and instead return an error indicating such, ie:

message.ts:10:11 - error TS2445: Property 'secret' is protected and only accessible within class 'Message' and its subclasses.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions