-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Add conditions to check for class extension or implementation of prim… #55712
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
Conversation
This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise. |
The errors are no longer exactly correct. They should be
|
src/compiler/diagnosticMessages.json
Outdated
"category": "Error", | ||
"code": 2855 | ||
}, | ||
"A class cannot implement a primitive type like '{0}'. It can only extend other named object types.": { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this message refer to 'implement' both times? Seems like it should but I'm not sure.
"A class cannot implement a primitive type like '{0}'. It can only extend other named object types.": { | |
"A class cannot implement a primitive type like '{0}'. It can only implement other named object types.": { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, that looks a typo. 'implement' would be better in this context.
class C implements number { } | ||
class C2 implements string { } | ||
class C3 implements boolean { } | ||
class C4 implements Void { } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need for this test; there's no type named Void.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for catching that.
class C3 implements boolean { } | ||
class C4 implements Void { } | ||
class C4a implements void {} | ||
class C5 implements Null { } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
class C5 implements Null { } | ||
class C5a implements null { } | ||
class C6 implements undefined { } | ||
class C7 implements Undefined { } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
class C5a implements null { } | ||
class C6 implements undefined { } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
testing null and undefined doesn't feel relevant to this PR since the new errors don't apply. Can you grep for existing tests that have 'implements null/undefined'? If there aren't any, feel free to leave them here; otherwise, remove them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There aren't both. However, I would remove these cases because these tests aren't relevant to this PR as you said.
enum E { A } | ||
class C8 implements E { } | ||
|
||
class C4a implements void {} | ||
class C5a implements null { } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same comment for these; it's nice to have these tests, but if they're already in the test suite, you don't need to add them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to look up the case of class implementing enum. However there isn't any result found. this time, I would remove these cases because these tests aren't relevant to this PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
Sorry, looks like you’ll need to |
…itive types like 'number'
…ers created during git rebase.
1bb904c
to
3868ca4
Compare
Thank you for letting me know. I did I just fixed the error numbers during the rebase. Is it okay to (edit) |
fixes unresolved issues that discussed on #49208.
Example
Class implementing the primitive:
Actual behavior:
Improvement:
Class extending the primitive:
Actual behavior:
Improvement:
Notes:
gulp runtests
passed on current branch.implement
weren't covered by current unit tests. I added a new testtests\cases\compiler\classImplementsPrimitive.ts
to test classes implementing primitive types.Description
PR #49208 was initially created to address issue #46246 along with some related problems.
However, that issue #46246 has been resolved by a different PR. So, PR #49208 wasn't merged and It was closed while leaving some issues that had been newly found and discussed in this PR, specifically related to incorrect error messages where classes extends or implements primitive types, as shown in the examples above.
To address these issue, I have made changes to the
checkAndReportErrorForUsingTypeAsValue
function, which has been responsible for these kinds of errors since #46357 was merged.I have added new functions,
isExtendedByClass
andisImplementedByClass
which determine iferrorLocation
's grandparent containsSyntaxKind.ImplementsKeyword
orSyntaxKind.ExtendsKeyword
. If a condition evaluates to true, the relevant error message is emitted.I would appreciate any feedback!