Skip to content

Improvement suggestion: Accidental assignment instead of == when using .filter() is not reported as compile error #36529

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

Closed
AntoninBrettsnajdrONSEMI opened this issue Jan 30, 2020 · 8 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@AntoninBrettsnajdrONSEMI

TypeScript Version: 3.7.5

Search Terms:
Accidental assignment using filter, boolean lambda, assignment lambda

Code

[1,2,3].filter(num => num = 2)

Expected behavior:
Compile error, since what I wanted was: num == 2

Actual behavior:
No compile error.

Playground Link:
https://www.typescriptlang.org/play/?ssl=1&ssc=31&pln=1&pc=1#code/NoRgNATGDMC6B0AzAlgGwC4FMBOAKAdgK4C2ABALwB8pRZ5pEAlEA

Notes
I am mainly a Kotlin/Java guy, so I am not sure if num = 2 is a boolean expression in JS, but anyway it feels wrong to me that this was allowed by TS, since it caused an unintentional silent bug in my code which took me some time to figure out.

@AlCalzone
Copy link
Contributor

I am not sure if num = 2 is a boolean expression in JS

It is truthy, which is why the expression is valid:
grafik

Not sure if it is possible to make the callback argument readonly, but this would probably break a lot of code.

@AntoninBrettsnajdrONSEMI
Copy link
Author

Ah I see ... btw I've also tried this now:
image
And it says "number". So that is IMO a good clue for the compiler since only booleans should be allowed here.

When I type this in the TS playground:

var y
let x: boolean = (y=1)

I get the compile error which is correct. Therefore if e.g. .filter() is expecting a function returning boolean, it should cause the compile error too, shouldn't it?

@AntoninBrettsnajdrONSEMI
Copy link
Author

Hah, but look at this: https://www.typescriptlang.org/play/#code/NoRgNABATJDMC6A6AZgSwDYBcCmAnAFAHYCuAthALwB8ERZl0AlIwLABQ7AbgIa4QCe7dNkwQAHgC5ajSjQBGAewXDuhBvhnVa-BiFZthogF5S6pKSVJy8m+UpVqKtS7edkKeoA

If I try it with lambda, it is a compile error:

var y
let x: () => boolean = () => (y = 1)
let z: (num: number) => boolean = (num) => (num=1)

but inside the filter(), no error reported ... strange 🤔

@AlCalzone
Copy link
Contributor

filter() does not expect the predicate function to return a boolean. An element is kept in the resulting array if the predicate returns a truthy value for the element.

@AntoninBrettsnajdrONSEMI
Copy link
Author

filter() does not expect the predicate function to return a boolean. An element is kept in the resulting array if the predicate returns a truthy value for the element.

Ah, ok, now I get it ☺️ . I was used from Kotlin/Java that it is a boolean, so this kind of error/typo can never happen there, but in the JS world it seems a bit more complicated I guess ☺️ . If it could be addressed somehow it would be cool, but currently I have no idea how. Maybe switching from truthy value to boolean, but I am pretty sure that it would break something.

@WhileTrueEndWhile
Copy link

#36472

@jcalz
Copy link
Contributor

jcalz commented Jan 30, 2020

Also relevant: #18497 and eslint's no-param-reassign rule

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Jan 31, 2020
@RyanCavanaugh
Copy link
Member

If you're new to JS it's unlikely you're going to find novel or unexplored issues in TS - please try Stack Overflow first. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

5 participants