You know that application that keeps you up at night hoping you won't have to change it? It seems like we all have one.
Mine was in JavaScript. It...
For further actions, you may consider blocking this person and/or reporting abuse
Besides the click bite title, I find the problems you're describing are not a thing that typescript itself is a solution too.
All of them are tooling and design problems.
Finding Existing 'Linking' Issues
Strongly-typed Variables
typeofis your friend heretypeofhas it strange behaviorsPromise<MyType<T>>. Is this more helpful maybe?Catching silly mistakes with ESLint
Introducing Classes
Eliminating Global State
Testability
In general, it sounds more like what the most significant difference here is that the team is more experienced and knows better what is needed. I'm not against TypeScript. It has its good things, but also it has its downsides. Like no abstract classes. Even OOP fanatics now say that composition is a better way then class inheritance (SOLID principle).
You are correct in the experienced aspect. However, the point in question here is that given a specific JS file with thousands of lines with existing issues, you need a way of making sense of it. TypeScript was that way, and the conversion process was what closed a multitude of bugs.
You'd be surprised what WebStorm has trouble interpreting with enough global state and dynamic code.
So, if there was a more efficient way of removing all bugs from the file, I'd love to hear it, but I will not debate that this was the answer I chose, and that it was extremely effective and needed.
I mean the first red flag for me here was: having a file with thousands of line. Again bad design or bad practice.
My point was again not saying that typescript itself is bad but it is not the holy grail.
Team communication, team standards and aiming for best practice are.
I'm not debating the answer here. For me it just looks like the wrong conclusion what helped is mad here.
But this is just my view and both are okay.
Of course the ideal is to create a big project with best practices from the beginning. I think the post is about a time that didn't happen, and how TS was the shortest path to a more robust codebase. Not that it's a holy grail, but that it helps make sense out of spaghetti.
Yeah exactly! The goal was: clean up the code and create best practices at least this is what I get from that post!
My argument was: As far as I can see for the reasons mentioned here in the post is not fully typescript but also eslint and the most important thing: the team! The team now has a better base to do great work! They are now more experienced and know their domain better! I can guarantee if they would rewrite it in plain javascript with eslint they would be as good as they are now with typescript. The only thing they have no bigger job security because 2 3 years from now we as a community will agree that typescript is not the thing we needed. Like it happened before with CoffeeScript and flow.
But maybe I'm completely wrong.
Typescript adds expressive compile time type info.
typeofdoesn't compare. I think the main point was that the extra safety from types made the other refactoring possible. That's not surprising since types are basically a kind of formal proof of program code.I still confused, for example, I create a method like this;
And backend data returns like that;
This is wrong I know. But I just imagined.
I used this data like that;
So, how can help us TypeScript in this case? I really don't have enough knowledge about TypeScript.
In the end, nobody replied to the question. You need to use a type guard for this. It is when static typing becomes dynamic. You have to manually check each field in the payload by
typeof. Check the official docs.typescriptlang.org/docs/handbook/a...
TypeScript does not do anything in this case. TypeScript only checks types at compile time while the types of the response from the backend is known only at run time. There are some tools/libraries that allow you to use TypeScript types at runtime like: github.com/gcanti/io-ts.
That's really interesting. I'm honestly not sure how I feel about that type of a library as it's not just a simple transpile down to JavaScript, but the injection of runtime type checking. I'll have to think more about that but I have a pretty strong gut negative for whatever reason. Thanks for sharing the library. I learned something new there.
So your aTypeScriptMethod would have a strict typing of number which means it will only accept that type.
This means when you later set age from dataFromBackend.age the let would be cast as a string.
Therefore when you try to pass the function a string you would see a compilation error, that would give you feedback saying you have tried to pass a string to the method rather than a number resulting in your code not compiling until you resolve the casting of age.
Owen - Either I'm misinterpreting your answer or it is simply wrong. Typescript only does compile-time type-checking. All it knows about data coming from outside is what hypothetical type you have assigned to your input parameters; but no run-time type checking or data-validation is embedded into the compiled code that actually handles the data: it has been compiled to plain JavaScript with no type checking.
In the example given Typescript is no help at all: the application would simply crash. I've seen this happen recently in an application taking data from a PHP backend out of our control. The data didn't conform to the data type we expected and our application bombed because someone forgot to add the proper checks. This was in QA so no big deal; but could have been really problematic if it had slipped through the net.
I think it would be unfair to call this a flaw in Typescript; but it is really important that people understand this. Typescript is not a substitute for doing proper type-checking/validation on data that is outside your control.
Could you share your eslint rules
I unfortunately cannot share any code with this particular article due to shared ownership. Was there some specific aspect you're curious about? I could talk about that in more depth.
I've been using typescript for about 2 years now and my lint rules are defined in tslint. I'm looking to convert everything over to eslint, so I'm curious to see what rules people are using. I am aware of airbnb rules but I'm interested in seeing what other things people typically lint
Great overview of the benefits of a type system (and bringing good design patterns) with an in-use code base.