E.g. if I need to add a Bar property to a Foo object, in plain JS you just do it as and when you need to without any extra hassle. This makes hacking something together really fast and low-friction which is nice.
Typescript wont let you do that though - you need to go off and define the Bar property on Foo which you might argue slows you down, or seems inappropriate if you only need Bar in certain circumstances and not every time or whatever.
Personally I find that the extra rigor that Typescript forces is way worth the extra hassle - it prevents so many subtle bugs, makes refactoring a lot easier, helps multiple people work together on the same code in a sensible way, and IDEs work so much better (in my experience at least).
Most popular libraries by the way all have the types defined already - check out https://definitelytyped.org/
So in development, I am hack together things and though the type checker complains and my IDE shows plenty of red marks, it will still compile.
Once I’m don’t experimenting, I fix the type errors, and refactor.
I just want to add a property to an object. All I have to do is add it to the definition, and TS will show me every location I have forgotten to set the Bar property, which means I can be confident it is set, so I do not need to write extra code that tests whether Bar is set.
Yeah this is what I was getting at - e.g. say I have 5 functions that all return Foo object, and for just one of them I need to add in an extra Bar property then I need to make sure that all 5 functions also do the Bar property, even if only 1 needs it (or make it optional, but that might screw with the semantics of the one function that does need it). So now I have a Bar on all Foos, even though only 1 function needs it - things start to get messy.
The alternative to changing all 5 functions is just to create a separate type for just that one function that needs Bar
Either way it is "extra work" if you are used to the fast and lose javascript way of just adding it there and then and not caring about the others.
I agree though that TypeScript is way superior as you point out it would just tell you where you screwed up and I would always start with a new TS project rather than JS today, but I still think there is a place for the raw & dangerous world of untyped JS for rapid and unsafe prototyping and hacking where TS can slow you down.
type Foo = {
Hello: string;
Bar?: string;
};
But if you really need separated types, the extension syntax is so short that you could define it in the function definition directly: type Foo = {
Hello: string;
};
function hello(myparam: Foo) {}
function world(myparam: Foo & {Bar: string}) {}People don't use static typing for writing code faster, they use it for code that can be READ (and understood) much faster, especially by somebody who doesn't work with that specific piece of code every day.
I think it's generally agreed that in most situations, reading code is much more common than writing code, so static typing really make sense.
Only in the beginning / short-term when the project is new, small, and fits in your head.
IMO doesn't take much more code and complexity for it to switch over to statically-typed code being faster to write.
As an extreme example, I run an online strategy game implemented in Elm. The game only supported three melee classes but the community made really good points about how ranged classes should work.
I came back to a codebase I hadn't touched in over a year, picked a spot in the code where I knew my physics would have to be updated to handle projectiles, started implementing it, and I basically followed the compile errors until my physics system supported projectiles.
Had it been dynamically-typed, this kind of refactor/overhaul would have required me to recredentialize in much more code and either rewrite tests and/or manually test my code branches at runtime to track down errors. It would have taken much much longer.
You nailed it.
I agree, this is the biggest benefit of static types.
I worked at on a several hundred thousand line JS code base at a company, and people were passing objects around, and it was extremely painful to trace code and figure what the structure of the object was. I had to set a breakpoint in the browser, and inspect the object during runtime. Moreover, some fields would randomly be missing, because the field wasn't necessary for that instance of the object. It was infuriatingly maddening.
I ended up spending a lot of time adding Flow types to it, of the form:
type Foobar = {
someField1: string,
someField2: number,
someField3: Baz,
fieldThatIsNotAlwaysThere: ?Qux
...
}
The untyped state of the code base gave me an extremely hard to resist to add Flow static types wherever I could. I also added a step to the pipeline (with my manager's support) that would break the pipeline and make it impossible to merge new code, if it didn't have Flow static types (and I used flow-coverage to make sure the "any" keyword wasn't being used excessively to side step Flow type checking). I was told by some of my teammates to stop forcing types down their throat. I eventually spent so much reworking large parts of the code base, and adding static types to it that my other work suffered (and I wasn't putting as much time as I should have into it), that I was told to stop spending so much time on adding Flow static types. But it was hard to resist the temptation. When I had to implement a new feature / change a file, I would add Flow types to it, and then be drawn to adding types to the various other files that it connects to (imports from, passes data to, etc). They fired me in the end, for that (not prioritizing the things I was supposed to do well enough) and other reasons (was going through relationship issues and eventually a bad breakup, which caused associated psychological/personal/self-care issues).I've learned my lesson. Dynamic types aren't my cup of tea, and I find dynamically typed code to be repulsive and quite nauseating.
This problem can occur in statically-typed languages too, such as if you have a class to model your entity, but in this instance you only need certain fields to be hydrated. The other fields may be set to null.
This might only a problem if the half-populated instance is 'leaked' to somewhere that expects a fully hydrated instance. Thinking about it though, it's possible that null could also be used to represent a null held on the database, rather than not hydrated.
The ideal solution would I suppose be to roll your own type for this semi-hydrated data, but that's not always possible/convenient.
Example: https://github.com/microsoftgraph/msgraph-sdk-dotnet/blob/de...
`let draft : Partial<Foo>;` says that 'draft' is shaped like a Foo, EXCEPT all of the fields are optional.
If you try to access a field on draft that doesn't exist on Foo, it's a ts error. If you try to pass draft to a function that takes a Foo, it's a type mismatch.
I would love to have static types in JS, but not at the expense of node.. and not just so people who are in different situations than mine are happy, because I act as if I was in their situation.
What do you mean by this? I use TypeScript with node all the time.
I personally love TypeScript, but I do have one big hang up. Janky builds in testing. Can't explain it, but sometimes the guarantees TS is supposed to provide don't compile over. Mainly while using nodemon or another auto compile. Have only ran into it a couple times, but it mainly crops up during module linking.
Also keep in mind you can use as modules via require('') and skip adding types to a js lib. Be sure to add --module commonjs or the equivalent in config.
Why I like TypeScript for myself is writing modules and api's. Mainly: inline documentation. And the pursuit of more of a deterministic outcome in your programs. Still the linking issue bothers me.
If it seems more hassle then it's worth for what you're working on, the rest of your team isn't bothered, and your clients are happy you don't have to worry too much. I still throw in vanilla JS on other smaller projects every once in a while for those reasons.
[1 EDIT] at most? hahaha.