DEV Community

Cover image for TypeScript is more than you think

TypeScript is more than you think

Pragmatic Maciej on February 25, 2020

TypeScript - commonly known as JS and additional type annotations, it's correct way of describing it, but hides the potential which lies in the lan...
Collapse
 
mgutz profile image
Mario Gutierrez

Thank you Maciej! The type system seemed overly complicated until I read this. You made it understandable by comparing snippets of the type language against Javascript idioms. It's all starting to make sense.

Collapse
 
damxipo profile image
Damian Cipolat

Several days ago I have been reading about typescript and I still can't find the advantage to use it, I was looking for that maybe it could be a great tool for handling numbers but funny there is no implementation of float, double etc. only "number" maybe you could guide me if there was something in typescript to handle numbers more precisely. for example differences of decimal places of integers, of positives of negatives etc.

Collapse
 
macsikora profile image
Pragmatic Maciej

The question is advantage over what? You need to consider that TypeScript is very pragmatic language in terms of its connection with JavaScript. The data level is not touched, it means that TS has the same primitive types as JS has, but TS makes them explicit.

TypeScript is JavaScript with additional compile time processing of the code. The article focus at this additional type level which is added.

In terms of numbers, JS has primitive number type, TS follows that to be fully compatible. If you want to have language which solves JS issues in numbers you need to take a look at languages which are not compatible with JS. And there are few - Elm, ReasonML, PureScript. There you don't have JS issues.

So TS is natural choice for JS devs, as you know most of the language day one. Its much harder to migrate the whole team to some fully functional language, therefor TS remains a sweet spot of productivity and reliability.

Collapse
 
dankwansere profile image
Dankwansere

Well written article, I enjoyed reading this.

But there is an error or misconception you made in this article that I should point out.
You stated the following

type X = 1; /* is TSts🟦 equivalent for JS🟨 : */ const X = 1;

Declaring "type X = 1" in typescript is NOT equivalent to "const x = 1" in Javascript.

The reason for this is because in Typescript, declaring a variable with a type only, prevents you from changing the type of the variable thereafter, but it does not prevent you from assigning a different type of value to that variable.
For example if I do the following in Typescript
let age:number = 5
I can later set age to a different number without any repercussions. But I will not be able to assign a string to age.

As for const (which works the same in JS and TS) once you declare a variable with const you cannot re-declare it to something else even if it's of the same type. For example If I was to do

const age:number = 5(typescript)
or const x = 5(Javascript)
I cannot re-declare age or x to a different number later. You would receive a compile time error in Typescript or a Runtime Error in Javascript. Const keyword is more equivalent to the "final" keyword in Java

Collapse
 
mateiadrielrafael profile image
Matei Adriel • Edited

You can achive the so called HOF with a global URIToKind interface, look at how fp-ts does it, its pretty hacky but its the most we can do

Collapse
 
jwp profile image
John Peters

Thanks Maciej, this is great news!

Collapse
 
jrumandal profile image
John Ralph Umandal

I finally learned something concrete about TS capabilities.
I got quite impressed. Thanks

Collapse
 
macsikora profile image
Pragmatic Maciej

Thank you for reading! If you want I have some series with advanced typescript Q/A, check it out - Typescript Exercises

Collapse
 
declantreanor profile image
declanTreanor

Thanks for that great article! I've spotted a typo, near the start :'lannguage'

Collapse
 
macsikora profile image
Pragmatic Maciej

Thank you very much

Collapse
 
seyyedkhandon profile image
DX Knight

Doing functions and map,filter,reduce etc... with types? that was insane😍
Thank you, it was great article with a different perspective that I had.🙏

ps. fix example in loops:
i<= 6;
should change to 5 or i< 6;

Collapse
 
davestewart profile image
Dave Stewart

Brilliant! Great one-stop-shop for all the TS funkiness. Thanks!