r/programming • u/DanielRosenwasser • Oct 01 '22
Ten Years of TypeScript
https://devblogs.microsoft.com/typescript/ten-years-of-typescript/192
u/Askolei Oct 01 '22
I'll give credit where it's due: thank you Microsoft for making JS development slightly less abhorrent.
153
u/butt_fun Oct 01 '22
slightly
Huge understatement here. TS took one of my least favorite languages to one of my favorites. TS lets you get almost all of the flexibility of JS but without any insanity
65
u/useablelobster2 Oct 02 '22
but without any insanity
There's still some insanity, because of the underlying JavaScript which is what actually runs.
My go-to example is iterating over a typescript enum, only to find it has twice the number of elements you expect. Because it maps the numerical value to the string value, and string value to the numerical value, in order to bodge enums into JavaScript. Of course that isn't an expected use-case, but it is something that logically makes sense but gives gotcha results.
And I've also found libraries whose official types are just plain wrong, and so need to be heavily coerced using debugging to figure out what object I've actually got.
But yes, little bug bears aside, it's pretty excellent. Performant too, tsc is a work of programming art.
28
u/aiolive Oct 02 '22
enum is maybe the only mistakes of TS where they pursued convenience over staying true to their purpose. An enum is not merely static type that may be stripped away to become JavaScript. So if you don't like paying the price that come with them, better avoid them. The other problem about JS libraries typed separately is not really a problem with the language itself.
3
u/useablelobster2 Oct 02 '22
I still use enums, because I know their gotcha now, and can hedge against it (just slice out half the length if you want to iterate).
The types issue is something I would call a language deficiency, due to the fact that Typescript is just a nice fantasy. It doesn't actually exist, only JavaScript does.
Most strongly typed languages I can't get ahold of something which is a different type than expected, the compiler says no. Typescript, because of its limitations, does have that issue. There are no incorrect types in C# packages, or Java packages.
2
u/jmazouri Oct 02 '22
They realized this mistake at some point and introduced
constenums, but the default behavior still generates a runtime object, and it's nearly impossible to take that back nowadays.Not sure what a modern "enum" would look like in Typescript but I guess they'd likely be a built in type, like
Pickrather than a new language construct, maybe with some clever leveraging of ES6 symbols for the runtime component.2
u/codepb Oct 02 '22
I've not yet come across a situation where a union of string literals isn't a better option than an enum. You don't need an import and it's just pure JavaScript so conforms to not needing the type.
12
u/ApatheticBeardo Oct 02 '22
Rule number one of effective TypeScript is avoiding features that generate code.
Read: enums and namespaces.
3
Oct 02 '22
There's still some insanity, because of the underlying JavaScript which is what actually runs.
This is definitely true, but you can at least ban most of the insanity using strict ESLint rules. The problem is that you kind of already need to be experienced enough to know how to set up ESLint and which rules to enable first, so it's not much help for beginners.
I think more integrated tools like Deno and Rome (if it ever materialises) should help with this sort of thing.
1
u/hekkonaay Oct 02 '22
Agree with most things, but tsc is far from performant. Most people avoid it by only using tsserver in their IDE for type checking/autocompletion/go to definition/etc., and esbuild or swc for transpilation.
14
u/HDmac Oct 01 '22
It's like all of the C# with none of the visual studio. (VSCode is awesome though š)
29
u/TheWix Oct 01 '22
It's better than C#. It's type system is way more powerful.
8
u/HDmac Oct 01 '22
Tell me about it, our C# code has a custom rolled library that enables discriminated union types and it's got to be the ugliest thing I've ever seen.
If only the node vs .net core performance was there š
5
u/STNeto1 Oct 01 '22
You can create type aliases, like type T = string, you can interact with complex types with stuff like Pick, Omit and Partial.
But probably the most powerful type resource is inference, if a function returns 1 + 1 the type system is smart enough to understand that the return type is a number. You can do the same for union types. You can infer the lots of stuff as well.
You can check typescript type challenges to get the feel about the type system.
4
u/phillipcarter2 Oct 02 '22
A core F# project with the domain modeling that exposes it as "just objects" is probably your best bet here. Plays to the strengths of the platform.
3
u/HDmac Oct 02 '22
Oh no, the custom lib is written in C# to emulate some of F# functionality š
2
u/phillipcarter2 Oct 02 '22
Woof.
3
u/HDmac Oct 02 '22
Yeah, last guy we hired to help out with it quit after two weeks. Lead dev can't fathom why using this lib is a bad idea.
2
u/TheWix Oct 01 '22
Discriminant unions in C#? Do you have to cast at all yourself? Do you guys have serious performance requirements?
4
u/HDmac Oct 02 '22
No casting, return types look like: CustomType<T1, T2>
Some guy wrote a lib that includes this and F# style functions a long time ago... I
1
u/douglasg14b Oct 09 '22
The type system definitly is, but as a unit they are better for different purposes.
I write ~50/50 C# & TS on both FE & BE (for both) these days, and while I regularly lament C#'s much more structured language and more limiting type system, it's still far superior as a server-side language.
So, I agree that TS type system is much better, it's more powerful, and once you start "thinking in TS language features" you really catch your wind and go ham. It's fun, even. However, as a whole C# & .Net Core are still far superior as a backed language.
4
u/EABadPraiseGeraldo Oct 02 '22
My guy VS is one of the best IDEs out there, especially if you care about anything Cpp.
1
u/HDmac Oct 02 '22
Well we weren't talking about Cpp but even over on that subreddit VSCode edges Visual Studio out on popularity. https://www.reddit.com/r/cpp/comments/srq6ek/what_is_your_favourite_ide_for_c/?utm_source=share&utm_medium=mweb3x
1
u/EABadPraiseGeraldo Oct 02 '22
True however your comment implied VS is somehow not good . Iām surprised by the results on the poll as the debugging tools available on VS are unmatched by most IDEs, a text editor like vsc doesnāt even come close.
1
u/HDmac Oct 02 '22
Sluggishness, freezing and crashing are frequent problems with Visual Studio in my experience.
1
u/EABadPraiseGeraldo Oct 02 '22
I work with VS on multiple machines and itās very snappy on all of those.But I wouldnāt be surprised if it happened to me after a bad update cuz thereāre so many different components that run simultaneously that thereās something bound to go wrong.
1
u/HDmac Oct 02 '22
thereāre so many different components that run simultaneously that thereās something bound to go wrong.
Precisely
1
u/douglasg14b Oct 09 '22
Now do this poll again for the primary audience, C# devs.
Also, notice how the comments have a different tone than the poll would suggest?
This is a common subreddit phenomina after 50-100k subs where transient or edge-users make up a significant number, even the majority, of the users of a sub.
Which means it's quite likely the poll has more responders that are not C++ devs, but who use it periodically, or are learning/interested. In other words, there is a significant bias for the lowest common denominator among devs, which is VSCode.
7
Oct 02 '22
Whaa? Visual Studio is amazing. I use it and Code at work but vastly prefer VS over Code for anything beyond individual file editing or opening a ton of files in bulk.
2
u/RudeHero Oct 02 '22
I'm an idiot- What do you all use for typescript debugging? What I've looked at steps me through the unreadable transpiled js
14
u/ff3ale Oct 02 '22
Generate mapping files?
1
7
u/DmitriRussian Oct 02 '22
Havenāt programmed in JS for a while, but usually you can generate source maps with JS so you have mapping from the obfuscated code to the actual source before compilation
0
u/IceSentry Oct 02 '22
Unreadable? Are you debugging minimized files? The typescript output has always been really readable for me since it's pretty much just the normal code but without type annotations.
4
u/Paradox Oct 02 '22
By just adding types? Or are you confusing TS with ES5, 6, and variants thereafter
13
u/butt_fun Oct 02 '22
Just the types. ES6 was also huge, but even untyped ES6 was still awful to maintain
(Not sure why you included ES5 - that was the still the almost universally recognized dark ages of JS)
9
1
Oct 02 '22
I suspect many feel this way because they have a background in either C# or Java and TS eases a simple transition from backend web dev to frontend we dev.
2
u/i_have_a_semicolon Oct 02 '22
Speaking from my personal experience of working in JS for 7 years before I switched to TS, I disagree. It simply means I don't need to flip between as many files to know I'm doing things correctly.
-8
u/Worth_Trust_3825 Oct 02 '22
TS lets you get almost all of the flexibility of JS but without any insanity
That's false. Typescript does nothing to that insanity besides add a cute linter, that's also turing complete for what ever fucking reason.
10
u/TheWix Oct 02 '22
I wouldn't agree that TS kills all the JS insanity, but it is more than just a cute linter.
-6
u/Worth_Trust_3825 Oct 02 '22
It's just a cute linter. It does not fix any of the issues javascript has, and even encourages them by permitting type algebra.
6
u/TheWix Oct 02 '22
A big issue with JS is a lack of a type system.
Do you mean an algebraic type system? Not sure what you mean by type algebra?
5
u/Parkreiner Oct 02 '22
Type Algebra
Sorry, what do you mean by this? I'm not quite sure what this is, or why it'd be a problem. Do you mean algebraic sum types (unions)?
-8
u/dravigon Oct 02 '22
Yea ppl over hype TS
3
u/TheWix Oct 02 '22
As a language on it's own it is pretty solid. I used to prefer languages like C# but Il now I prefer TS. The type system is way more powerful.
-8
u/thoomfish Oct 02 '22 edited Oct 02 '22
Just needs a standard library and I could even see it rising to the level of "something I could recommend to other sentient beings with a mostly clear conscience."
It is batshit insane that if I want to shuffle an array in JS in 2022 I have to implement it myself or pull in a dependency.
2
u/HDmac Oct 02 '22
I've never in my professional career needed to randomly shuffle an array. Why would that be in a standard lib?
2
u/Mognakor Oct 03 '22
Shuffling is a generic algorithm thats not domain specific, thats exactly what belongs into a library.
As for an example when to shuffle:
You have a collection of items with variable size. The size of items is related to their neighbors, i.e. big items likely are next to big items, small items are likely next to small items. You cannot easily pre-determine the actual size of the items.
Now you want to split the collection into N batches for multithreading. Shuffling them randomly is a good attempt at removing the size-locality relationship.
1
u/HDmac Oct 03 '22
There are many generic algorithms that aren't in libraries because they're simply not used often enough to warrant that.
per your example - forking/worker processes is rare in Typescript.
1
u/Mognakor Oct 03 '22
Examples for such algorithms that are also not domain specific?
Considering the current state of JS i'd rather have it a couple superflous library functions than lacking what i need.
1
u/HDmac Oct 03 '22
Examples for such algorithms that are also not domain specific?
Inserting or removing a single element from an array.
1
34
u/Funcod Oct 01 '22
These job descriptions which asked for ten years of experience, five years ago, are retroactively finally relevant.
17
u/JimK215 Oct 02 '22
A friend of mine sent me a screenshot the other day of a job post asking for 10+ years of Kubernetes experience... Kubernetes was released in 2014. He was excited to have found one in the wild.
33
u/Professor226 Oct 02 '22
When i interview programmers i ask what their favourite language is and why? Because i want to know if they understand what features a language has. I then ask what their least favourite language is. Javascript. Every time.
28
u/vetinari Oct 02 '22
Javascript. Every time.
Oh kids these days; obviously they never had to write anything in VB or VBA ;-).
9
5
2
Oct 02 '22
I remember my first year in college dealing with VB. I taught myself C and C++ in high school and was so used to those and was so excited to start doing more work in them in college only to be smacked in the face with (I think) VB6.
My enthusiasm died a little that day.
13
6
u/IceSentry Oct 02 '22 edited Oct 02 '22
There's plenty of people that are willing to claim js as their favorite language. I think this is just selection bias based on the context of the job you are interviewing people to do.
There's plenty of languages that are worse than js.
1
7
3
u/JackAuduin Oct 02 '22
No one ever said PHP?
3
u/theavatare Oct 02 '22
Php has types now alsoā¦
1
u/dpash Oct 03 '22
The only missing part is generics, but phpdoc allows various tools to pretend they do.
5
u/dxk3355 Oct 02 '22
I didnāt realize typescript was only created in 2012. Now I donāt feel bad not knowing it yet.
5
u/Dravorek Oct 02 '22
Typescript is pretty neat. It made frontend scripting bearable to someone very used to static typing.
I heard python is also getting some more type annotation features.
I don't know if there's any big dynamically typed language left that doesn't have some optional or gradual typing implemented.
7
Oct 02 '22
Python's static typing is much worse than Typescript in practice unfortunately, even though it has a couple of features that are more advanced.
The issues are:
Multiple conflicting type checkers. The most popular (MyPy) is not very good. Pyright is excellent but I don't know how they expect things to work properly if your dependencies are using a different type checker to you. In practice that issue doesn't seem to crop up too much because...
Half of the Python ecosystem doesn't understand why static types are essential so lots of your dependencies won't have any types anyway, and lots of them use horribly dynamic patterns that are impossible to type anyway.
There are some significant gaps - most egregiously you can't type kwargs properly and lazy Python developers love kwargs, even though you should almost never use them.
I don't know if there's any big dynamically typed language left that doesn't have some optional or gradual typing implemented.
I think you're right. If any dynamically typed language becomes popular enough that a big company makes the mistake of going all-in on it then they'll inevitably reach the point where creating a static typing system from scratch is less work than maintaining a dynamically typed codebase.
16
Oct 02 '22
Typescript is awesome. I use "do you use Typescript" as a proxy for "are you sane and competent" in interviews. If you have tried Typescript and rejected it then frankly you shouldn't be programming. Looking at you, my co-workers.
1
-3
-13
u/SalamiSandwich83 Oct 02 '22
Imagine, 10 years for a half backed shit based on a half backed shit. Lmao.
0
112
u/modernkennnern Oct 01 '22
I saw a talk on NDC Oslo 2 days ago where Richard Campbell (incredible speaker, particularly in real life) talked about how Microsoft was incredibly surprised by just how popular TS became right of the bat, and how the open source nature of the project affected it's success, that it - to no small degree - help steer Microsoft more towards Open Source projects.