4 ms·
IMO, Yes (usually). Let me elaborate. If you are starting a new project, yes use typescript over JS. And configure it with all strict options. This doesn't m
by sethc2 7y ago
IMO, Yes (usually).
Let me elaborate.
If you are starting a new project, yes use typescript over JS. And configure it with all strict options.
This doesn't magically give you well architected and well-tested code, it still takes a good engineer to do that. But what it does allow is for the engineer to refactor the code to support new functionality with more speed and confidence, rather than shoehorning it in in fear of breaking something or it taking too long. Furthermore, as people join your team, it will be a lot easier for them to figure out where some piece of data is coming from and how the code flows from one spot to the next.
If you have a massive JS project and you want to convert it to typescript. The answer might be no. Oftentimes when people port a JS project to TS, they end up using non strict typescript, and you get the syntactic overhead of writing TS, without really any benefit because nearly everything is any typed. If your only concern is a better codebase, and it doesn't matter how long it takes, then sure it is worth it, but we all know that is never our only concern. It still might be worth it, but I can almost guarantee you from experience, you will still end up with TypeErrors in your console at some point, because people will have just any typed things.
Also if you're writing a tiny node script in a single file, to do some simple operation, then no it is probably not worth setting up a command to compile it and a tsconfig to configure it. Unless it is doing multiple things, and/or someone else in the future will have to use it, then it is probably still worth it.
- sethc2 7y agoOh one other potential drawback. Typescripts inferred types, can do some crazy things, and can be very powerful. This to me is really fun, but has definitely caused me when I first started using it to get lost in having fun doing crazy things, to ensure my strings were exactly some set of values depending on what some other value was, when a simple unit test and a simple "string" type, would've been a lot quicker and less confusing.
- samhh 7y agoIf the strings are static you can define a type that's a union of string literals, create a tuple containing them all, and then a type guard function that simply checks the argument against the array. If the strings are dynamic then you're better off looking at a library like newtype-ts, but it's not very ergonomic compared to the likes of Haskell/PureScript so use sparingly.
- no_wizard 7y agoWhy not just use an Enum? It will only accept the values of that enum. https://www.typescriptlang.org/docs/handbook/enums.html#enums https://www.typescriptlang.org/docs/handbook/enums.html#enum... https://www.typescriptlang.org/play/index.html?useDefineForClassFields=true&preserveConstEnums=true#code/KYOwrgtgBAsgngZQC4CcCWIDmBnKBvAWACgpSoAxAeUqgF4oAiAIwEMUGAaYsqAIQEEASnUYAzAPbjO3MlUoDh9BhPGt2xAL7FiosCADGSNOJBQTAGzgBRcBH4pMACgBuLcwC5YiVBhwBKfBlSfRNscXNgADpzcScGJAALNFwAdzRzczMQSygkFgBrYCyi8VFchKLCuFxRFHFoeGR0LGwGPwBuIKgQkDCI6NiXNzpaekafFsiFDs1tIgtrW3s4xOSoNIyoJiKWU2AUOvYZ+ezFyGXHceacKaEAgHpHqAAVJNw1kHEkKF2ofcOgA https://www.typescriptlang.org/play/index.html?useDefineForC...
- untog 7y agoJust my personal preference but using these string union types means that you don’t have to import the enum. Plus it’s less verbose and translates back to JS better than an enum does. Really just a matter of preference though.
- mattmanser 7y agoErrr, you wouldn't do crazy stuff like that in C# or Java, so no, this isn't a good example. It's just you doing something stupid thinking you were being clever, as we all have.
- sethc2 7y agoYeah the stuff I was doing was more to see how crazy of things we could do with the type system. Like I said just fun. But probably a waste of time other than the learning. Just a warning that is tempting to do crazy (but stupid) things. One kind of weird thing we did, was we had these JSON Schema definitions that were const, so all the strings like "type" were of a literal string type. Then using recursive type definitions, and the `extends` key word we were able to extract the actual shape of the data into a type. Not exactly that useful, compared to typing it out, but it was still kind of cool.
- dfee 7y agoThis is interesting to me. How did you decide that you were over investing in types?
- rubber_duck 7y agoI have a use case where I'm using typescript to get type hinting in the model layer (mostly just interfaces describing API schema) and reducers - it helps document store structure, catch typos and quickly lookup available properties/see model structure. The rest of the project is ESNext/babel - we have to interop with an existing codebase, typing everything is very low value compared to overhead - similar to writing unit tests for UI components.
- agumonkey 7y ago> This doesn't magically give you well architected and well-tested code, I'm shopping for this. Also property based testing.
- mattmanser 7y agoThis seems more like dynamic vs static language advantages, rather than js vs ts. I'm a big static language proponent, but we have to ask ourselves, is typescript simply popular as it's kinda statically typed?
- dragonwriter 7y ago> is typescript simply popular as it's kinda statically typed? Isn't that the overt main point of TS, hence the name?
- thawaway1837 7y agoThis ignores the many actual situations where people and teams and companies have migrated from JS to TS and seen immediate benefits and improvements, and discovered bugs simply as a by product of the migration, even if they’re not using very strict typing.