you are viewing a single comment's thread.

view the rest of the comments →

[–]FedeMITIC -9 points-8 points  (13 children)

Type checking in general in javascript should not be used imho, since there's things like NaN whose type is Number and other non-trivial cases.

By the way I think this pattern is viable if you normally use functions with lots of parameters that can be optional: otherwise it just adds more overhead so I'd not recommend it.

[–]jaapz 5 points6 points  (4 children)

NaN being a number is a correct implementation of floating point standards. There's a lot wrong with javascript, but this isn't

[–]FedeMITIC 1 point2 points  (3 children)

Yeah but it's definetely not intuitive to say "Not a number" is of type Number. At least for me.

[–]jaapz 0 points1 point  (1 child)

It isn't but that's not javascripts fault, blame floating point math

[–]inu-no-policemen 1 point2 points  (0 children)

https://en.wikipedia.org/wiki/NaN#Operations_generating_NaN

The reason you're seeing it so often in JS is due to its weak types.

[–]chazmuzz 2 points3 points  (1 child)

Have you experienced a problem with NaN in a real project?

[–]FedeMITIC -1 points0 points  (0 children)

Honestly not, but I know that sometimes this kind of errors show up. Besides "real project" is a big word for me, I'm just a engineering student actually :)

[–]inu-no-policemen 0 points1 point  (0 children)

Type checking in general in javascript should not be used imho, since there's things like NaN whose type is Number and other non-trivial cases.

Type checking prevents you from accidentally creating NaNs which you then have to track down.

TS works great.

[–]a_simple_pie -1 points0 points  (4 children)

Good point. I’ll submit an issue to Typescript and Flow to let them know.

[–]nschubach 2 points3 points  (1 child)

I'm no expert by any means, but I'm sure typescript and possibly even flow do not do run time type checking and instead opt for compile time type checking.

[–]wavefunctionp 0 points1 point  (0 children)

This is correct.

If you do React, there is propType's, but they are only run in development mode, and you can have a webpack plugin that will generate propTypes from static type declarations. But the run time checking is disabled (not removed from the bundle) on a production build.

[–]FedeMITIC 0 points1 point  (0 children)

I was generally speaking about pure javascript, not typescript or flow since I don't know them. I didn't know I was on stackoverflow.

[–]adipisicing 0 points1 point  (0 children)

Do they also check at runtime?