Legal in Canada too. I just assumed it's legal everywhere because logically it's a safe operation.
My favourite is a one way /one way intersection in my town where I will turn left from the far left lane into the far left lane. It feels very British.
Oh no this is really far off topic. I'm a JS developer in robotics and I keep getting tempted by the allure of TS. The only reason I don't take the plunge is that I'm the only developer and mitigate most typing issues because I'm so intimate with the code. I suspect as a team grows I'll regret not going TS :)
I'm not a 'Typing is Safety' person - languages I use tend to be strongly, dynamically typed. At least one study has shown more errors come from other issues other than type mismatches and that's also my own experience.
That said: as my ES2017 app gets more complex, a little type safety could be useful. I think it might catch more stuff - eg, earlier this week I tried to use a Promise (because I forgot an 'await') and then got a bunch of NaNs on invoices (the unit tests had the await, so passed, the route didn't).
Renaming something to .ts and adding a quick compilation step doesn't seem like a big deal and would catch that error. Typescript also has a MASSIVE amount of community support around it. So yeah: still don't believe 'typing is safety' but will likely end up using TypeScript by the end of the year.
IMO the static analysis that becomes possible from using types is more useful than types themselves.
My favorite JS gotcha is the number of special values a variable can take on. NaN, Undefined, null, Infinity, -Infinity, maybe more? Also the fact that arrays and objects are always truthy. Honorable mention for the seemingly random results of concatenating strings. I'm tired of getting burned by these design flaws and if it takes strong typing to be protected from them than so be it.
I came to JS after spending years in C# and Java, and so I liked the dynamically-typed nature of the language. It feels a lot freer, to me. Of course I understand why people think static typing is important, but going back to that way of denoting what everything is just seems so onerous.
But even regardless of that, what I really hate is how these typed JS variants introduce so much extra syntax and -- sorry for my phraseology -- crap into the language.
Here is an example of Flow:
declare function isMap(m: mixed): boolean %checks(m instanceof Map);
You can call that whatever you want, but JavaScript it ain't.
Flow doesn't require you to write what's above at all for the most part, and it's type inference and flow analysis is amazing. Your applications code will have few type hints if that's what you want, and your core libraries can have the extremely specific types as what's show in the parent post. I dunno, I like TS and Flow, and gradual typing with excellent inference means it rarely gets in my way, even when calling out to JS libraries that are as dynamic and loose as you can get.
Sure, but that isn't JavaScript and isn't needed at runtime anyway. It's literally stripped out, not even compiled (unlike TS)!
Flows type system is amazingly powerful, too, which I adore. So I'm biased; being able to emulate HKTs with real type checking gives me the dynamic power of JS with the type safety I want; and it's an easier sell than Purescript ;)
I spend most of my time writing in Swift, The rest of the time I'm JavaScript, talk about a schism. I greatly prefer Swift, which is why I'm installing Typescript this week. Maybe because I'm less experienced with JS, but I make too many silly mistakes in it.
I know a lot of people love the freedom of JavaScript, but I also know lots of people write crappy code.
> incremental typechecking sounds small but isn’t. It pays huge dividends in developer productivity. As Dale notes, “Refactors that used to take weeks take days, sometimes less..."
Just out of curiosity, what is the basis of claims like that? What types of bugs does static typing prevent, and how pervasive are those bugs otherwise?
I encourage you to convert some of your JS code. In my experience converting something like 10k lines, this is absolutely true.
I convert using strictest compile rules + TsLint with most rules on. In those 10kish lines I found hundreds of potential bugs. I'm sure at least a third were real.
The biggest culprits in our codebase were classic type errors and improper use of truthy/falsey. Adding types enables a ton of static analysis like identification of broken conditional statements. I found over 100 if statements that were always true/false or otherwise ineffective.
Mostly the latter. With strict type checking enabled TsLint will warn you about impossible conditionals. You can go a step farther and force conditionals to return boolean true/false. This disables JavaScript's "truth/falsey" but prevents a whole class of errors caused by questionable design decisions... Like empty arrays being truthy.
From a refactoring perspective, typechecking makes the answer to the question "What is this change going to break?" much more obvious.
Combine that with Typescript's IDE support and a lot of the usual dumb refactoring mistakes just don't happen so you can focus on the actual algorithmic problems emerging.
Absolutely. But it only tells me where the code failed, not necessarily where my structural assumptions first fell apart. So I then have to walk up the stack looking for that point. It's pretty mechanical to do but typescript does it for me.
This also assumes you have solid test coverage which unfortunately isn't always true. Especially for code that is being refactored because it's becoming (or already is) unsustainable.
>In many typescript codebases (angular4) strict null checks is disabled befits are really minuscule.
I agree that --strictNullChecks is very useful if the codebase often assigns meaning to null/undefined but that's more of an overall design problem. I feel there's a lot of benefit outside of that but I'm generally all for explicitness in code so I might be biased.
>I am now doing an angular4 project and I do not feel any safer.
If it's greenfield development, you probably won't for now. The implicit type system is fresh in your mind and having to explicitly put it into code is just going to feel like it's slowing you down. I suspect the benefits will come later when someone who isn't intimately familiar is looking at it (could be a new team member, could be you after another project or a vacation).
>If you want to have real static typing use Elm, Reason, BuckleScript.
For sure. But the downside with these is the commitment required. Typescript is as low effort/commitment as you need it to be. And an experienced js dev can pick up the basics in an hour.
>Tooling is already bad on anything other than VS Code.
I've only really used it in webstorm (which also uses the typescript language service) and it's never been an issue.
Look at mister test driven development over here. Hey everyone this guy writes unit tests.
I'm joking here but I think that a lot of people ignore basics like writing unit tests, and that statically typed languages will break a lot more quickly and predictably for people who are lazier, since it enforces structure.
However, I agree with you. Also I think that the people who really push for static typing are the kinds of people who already would write unit tests.
We do. Most of our projects have 80%+ unit test coverage and decent amount e2e/acceptance tests.
The problem with TypeScript is that in most codebases you will need to disable useful type-checks. Without strict null checking, you will only catch trivial errors. Additionally, you will interface with a lot of JS code that do not provide typings.
> As Tom Dale, a senior software engineer at LinkedIn and a JavaScript luminary, more colorfully described it to me, “JavaScript is like the hippie parents who let you have premarital sex and smoke weed.” That sounds great, he notes, even liberating, but “eventually you resent that you didn’t have any structure and now you’re 32 and living in their basement.”
I don't mean to imply anything about this man's outlook on life from these quotes, but that's a mormon-worthy stereotyping of premarital sex and weed.
Depending on where you're from, marriage before sex might be the culturally-enforced default. Enabling weed even more so, which is unfortunate in general, but probably a healthy approach when it comes to kids.
The thing I like most about TS is interfaces. I use a single "options" parameter for functions whenever there are a bunch of parameters, and having autocomplete for it makes life so much easier.
25 comments
[ 3.4 ms ] story [ 75.5 ms ] threadMy favourite is a one way /one way intersection in my town where I will turn left from the far left lane into the far left lane. It feels very British.
Oh no this is really far off topic. I'm a JS developer in robotics and I keep getting tempted by the allure of TS. The only reason I don't take the plunge is that I'm the only developer and mitigate most typing issues because I'm so intimate with the code. I suspect as a team grows I'll regret not going TS :)
That said: as my ES2017 app gets more complex, a little type safety could be useful. I think it might catch more stuff - eg, earlier this week I tried to use a Promise (because I forgot an 'await') and then got a bunch of NaNs on invoices (the unit tests had the await, so passed, the route didn't).
Renaming something to .ts and adding a quick compilation step doesn't seem like a big deal and would catch that error. Typescript also has a MASSIVE amount of community support around it. So yeah: still don't believe 'typing is safety' but will likely end up using TypeScript by the end of the year.
My favorite JS gotcha is the number of special values a variable can take on. NaN, Undefined, null, Infinity, -Infinity, maybe more? Also the fact that arrays and objects are always truthy. Honorable mention for the seemingly random results of concatenating strings. I'm tired of getting burned by these design flaws and if it takes strong typing to be protected from them than so be it.
But even regardless of that, what I really hate is how these typed JS variants introduce so much extra syntax and -- sorry for my phraseology -- crap into the language.
Here is an example of Flow:
You can call that whatever you want, but JavaScript it ain't.A simple debugging output can attach the types visibly.
So... My question is why these statically typed JSs add such verbose syntax?
Even C++ has had auto to avoid irritating boilerplate.
Flows type system is amazingly powerful, too, which I adore. So I'm biased; being able to emulate HKTs with real type checking gives me the dynamic power of JS with the type safety I want; and it's an easier sell than Purescript ;)
I know a lot of people love the freedom of JavaScript, but I also know lots of people write crappy code.
Just out of curiosity, what is the basis of claims like that? What types of bugs does static typing prevent, and how pervasive are those bugs otherwise?
I convert using strictest compile rules + TsLint with most rules on. In those 10kish lines I found hundreds of potential bugs. I'm sure at least a third were real.
The biggest culprits in our codebase were classic type errors and improper use of truthy/falsey. Adding types enables a ton of static analysis like identification of broken conditional statements. I found over 100 if statements that were always true/false or otherwise ineffective.
I'm not sure whether that shows how good Typescript is or how bad Javascript is.
Combine that with Typescript's IDE support and a lot of the usual dumb refactoring mistakes just don't happen so you can focus on the actual algorithmic problems emerging.
I am now doing an angular4 project and I do not feel any safer. If you want to have real static typing use Elm, Reason, BuckleScript.
I do not trust MS. Tooling is already bad on anything other than VS Code. TypeScript development is slowing down from 2017.
Absolutely. But it only tells me where the code failed, not necessarily where my structural assumptions first fell apart. So I then have to walk up the stack looking for that point. It's pretty mechanical to do but typescript does it for me.
This also assumes you have solid test coverage which unfortunately isn't always true. Especially for code that is being refactored because it's becoming (or already is) unsustainable.
>In many typescript codebases (angular4) strict null checks is disabled befits are really minuscule.
I agree that --strictNullChecks is very useful if the codebase often assigns meaning to null/undefined but that's more of an overall design problem. I feel there's a lot of benefit outside of that but I'm generally all for explicitness in code so I might be biased.
>I am now doing an angular4 project and I do not feel any safer.
If it's greenfield development, you probably won't for now. The implicit type system is fresh in your mind and having to explicitly put it into code is just going to feel like it's slowing you down. I suspect the benefits will come later when someone who isn't intimately familiar is looking at it (could be a new team member, could be you after another project or a vacation).
>If you want to have real static typing use Elm, Reason, BuckleScript.
For sure. But the downside with these is the commitment required. Typescript is as low effort/commitment as you need it to be. And an experienced js dev can pick up the basics in an hour.
>Tooling is already bad on anything other than VS Code.
I've only really used it in webstorm (which also uses the typescript language service) and it's never been an issue.
I'm joking here but I think that a lot of people ignore basics like writing unit tests, and that statically typed languages will break a lot more quickly and predictably for people who are lazier, since it enforces structure.
However, I agree with you. Also I think that the people who really push for static typing are the kinds of people who already would write unit tests.
The problem with TypeScript is that in most codebases you will need to disable useful type-checks. Without strict null checking, you will only catch trivial errors. Additionally, you will interface with a lot of JS code that do not provide typings.
I don't mean to imply anything about this man's outlook on life from these quotes, but that's a mormon-worthy stereotyping of premarital sex and weed.