Skip to content

Private class field helpers throw a confusing error when in unbound method with undefined this #57609

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

Open
jakebailey opened this issue Mar 2, 2024 · 2 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@jakebailey
Copy link
Member

πŸ”Ž Search Terms

this undefined bind private identifier class __classPrivateFieldSet __classPrivateFieldGet bound unbound

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

https://www.typescriptlang.org/play?#code/MYGwhgzhAEBiD29oG8BQ0PQMQDcwgFcBTaAXmgEYAmAZgBYBudTAcyIBcA1fYgCgEoU0AE4cCwgHbR2ACwCWEAHS4eRBtAC+qLamDwJEdtABmiMtAlEA7nEQCmu-RHggiikPBa8ABqfiK2LlUBcwASZD8Ajm5CIgENb34HAHpk6AB5eAAHCAAaaABJaAATOWKJAHIjACM5CWKAQkcDI0CY4nNIttUUtIAVGWF4KwgALmgAYTAJCXgjUTBi6CzhOTx2EgBbIk3qomETIc3oaeh4aoArImAjKxl4CBJQSBhSpdmjYuvwUWg5dmazlc7k8Pm6sRC5HC4L4-AS-FQQA

πŸ’» Code

class Foo {
    #value = 1234;
    getValue() { return this.#value; }
}

const foo = new Foo();

console.log(`foo.getValue() = ${foo.getValue()}`);

// Oops, I didn't bind!
const getValue = foo.getValue;

// Throws: Cannot read private member from an object whose class did not declare it
console.log(`getValue() = ${getValue()}`)

πŸ™ Actual behavior

Throws Cannot read private member from an object whose class did not declare it.

But the actual problem is that this is undefined and I had no idea!

πŸ™‚ Expected behavior

A special error for this when undefined; IIRC there's no possible way this would normally pass, so a special error would be a lot more clear.

Additional information about the issue

No response

@fatcerberus
Copy link

For comparison, when targeting ESNext (i.e. native classfields), the error is instead:

Cannot read properties of undefined (reading '#value')

@jakebailey
Copy link
Member Author

Yeah, I wasted like 40 minutes of debugging the typescript-bot trigger code only to find that I had passed Azure Function's logging function down through the handler without binding it. An error like that would have been more helpful, though I do wish the native (and downleveled code) instead mentioned this...

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Mar 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants