feat: support error when comparing with object/array literals#45978
feat: support error when comparing with object/array literals#45978sandersn merged 5 commits intomicrosoft:mainfrom
Conversation
Jack-Works
commented
Sep 21, 2021

|
This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise. |
|
@Jack-Works please create an issue to track this. I believe @DanielRosenwasser has some experience about what issues we need to consider for adding a check like this. |
|
Honestly, if the thing on either side is an explicit literal, I don't think there's ever an issue with providing an error. |
|
Do I still need to create an issue since @DanielRosenwasser replied? |
sandersn
left a comment
There was a problem hiding this comment.
Request for one more test
(And I learned that this applies to ==, not just ===)
What test do you want to have? Yes, it applies to if (0 == false) {
// This condition will always return 'false' since the types 'number' and 'boolean' have no overlap.(2367)
// but actually true in JavaScript
} |
|
I want to test something that is (1) an existing type error (2) the new object/array literal error: const b = [1]
if ({ a : 1 } == b) { }That's the only addition. |
6866a85 to
29679df
Compare
|
I have resolved the conflict, can you give another review? thanks, @sandersn @DanielRosenwasser |
29679df to
00b2084
Compare
|
Thanks! I have rebased again to fix merge conflicts. |
|
Looks like a couple of tests still need their baselines updated. |
|
Should we...always show this to JavaScript users? |
Of course, should I do anything to enable this in JavaScript? |
|
@Jack-Works there is a set of errors to show to JS users in src/compiler/program.ts .. plainJSErrors, I think. Add it there and it will show up for all JS users. There might be cases where |
There're only binder errors and grammar errors in |
|
Technically, yes. Grammar errors already come from the checker. I vote that we do this for 4.9, after Typescript users have had a chance to report any problems with it. In the past, JS users have gotten irate when code they know to be correct gets a TS error put on it. |
|
I can't think any case that |
|
It could be surfaced as a suggestion, right? I do agree that we should be a little conservative on things like this for JS users. |
But that's definitely a mistake even you're using JS 🤔 |
|
Also, suggestions ought to provide a codefix. And their default UI in vscode is extremely subtle. |
|
In this case I cannot provide a code fix because it really depends on the intension of the code 🤔 |
|
I think we should ship this as an error in 4.9 for JS, perhaps with some more restrictions to cut down on false positives. |
|
Can you give an example of false positives on this error? 😂 I can't think any of it |
|
I commented on the PR. So far they all look like intentionally bad code, so there might not be anything to do. |
[1,2,3] == '1,2,3'
// -> true
({toString() { return 1 }}) == 1
// -> true
|