DEV Community

Cover image for Typescript: Introduction

Typescript: Introduction

Jatin Sharma on December 29, 2022

In this article, we are going learn about What is Typescript? What does it do? Installation process. After reading this article you will have a lit...
Collapse
 
nexxeln profile image
Shoubhit Dash • Edited

It doesn't add more features.

That's actually the point of typescript. It adds a type system but since its transpiled into javascript, they can implement latest ES6 syntax without breaking browser compatability.

Some of the features typescript had before javascript are classes, logical assignment operators, decorators (still not implemented in js afaik), optional chaining, etc.

Collapse
 
j471n profile image
Jatin Sharma

You are correct. TypeScript add static typing to your JavaScript code, while still maintaining compatibility with older versions of browsers. That's why you can use latest feature because eventually it will be transpiled to JavaScript. But I think mostly people use it because it can catch type errors at compile-time, rather than runtime.

Collapse
 
nexxeln profile image
Shoubhit Dash

Sure, but I was just pointing out your statement was objectively incorrect. Overall great post!

Thread Thread
 
j471n profile image
Jatin Sharma

Thanks mate, I have changed the statement. :)

Collapse
 
philipp__6424ee1faaca7b14 profile image
Philipp

Type safety is not really the reason for swtiching to TypeScript. If that was the only one, you'd be better off just using JSDoc and a good IDE.

Collapse
 
j471n profile image
Jatin Sharma

I agree with you that there are several reasons to use Typescript and Type safety is one of them.

Collapse
 
kolja profile image
Kolja

It took a while for me to understand, that Typescript is not for runtime, it is for devtime 😇

Collapse
 
j471n profile image
Jatin Sharma

Yes exactly, it lets you catch errors before you go to production.

Collapse
 
idleman profile image
idleman

TypeScript do not have types, it has type annotatations which is the worst of both worlds. No real type safety and slow development speed. if you want types, use a statically typed language, like rust or C++ and compile it to webassembly. If you want development speed and a flexibility, use JS.

Collapse
 
j471n profile image
Jatin Sharma

I partially agree with your statement. Yes, TypeScript does have types and type annotations, but it can provide real type safety. What TypeScript does is that it add types & type annotations functionality to JavaScript and It can also compile the code to JavaScript. In fact, many developers use TypeScript specifically for the type safety.

Collapse
 
idleman profile image
idleman

TypeScript cannot provide typesafety. Please do some low level programming before you say something so misleading like TypeScript has type safety, because it is simply incorrect - if you know how a computer works. Types has a fixed memory layout and a set of CPU instructions that effectively can operate onpon that type.

In TypeScript can you cast a picture to an integer, in real typed language cannot you do it. This is what typesafety means, you should be able to trust the most basic primitives at least.

Thread Thread
 
j471n profile image
Jatin Sharma

Correct me if am wrong here:

You stated that in typescript you can cast a picture to an integer.

In TypeScript can you cast a picture to an integer, in real typed language cannot you do it.

Can you explain how can you do that in typescript?

Thread Thread
 
idleman profile image
idleman
Thread Thread
 
j471n profile image
Jatin Sharma • Edited

This was happening because your cb() function returning any value. That's why it's not recommended to use any type.

Image description
typescriptlang.org/play?#code/GYVw...

But if you do like this then you will get error:

typescriptlang.org/play?#code/FAMw...

Thread Thread
 
idleman profile image
idleman

Turn on your brain please. I know how to fix so TypeScript catches the error, but this is not about making sure TypeScript catches the error, but instead make sure you can call a function and trust it returns the type you expect. And my example proves YOU CANNOT TRUST IT. Everything within a function is an implementation detail.

Thread Thread
 
j471n profile image
Jatin Sharma

Make sure you can call a function and trust it returns the type you expect.

That's what I am saying when you create a function cb() I can't trust on that function because it can return anything which is the main problem. That's why when you use cb() as number it doesn't show any kind of error.

If you want to trust a function to return what you expect, then you have to define them well (not like cb() for sure).

Thread Thread
 
idleman profile image
idleman

From a developer perspective, everytime you call a function should you 99.99% of the time never need to worry about the implementation. So you should never go to its definition to see how a function is implemented. See it is a black box. The only thing you should care about is its parameters and return type and be able to trust the compiler to enforce it (like every statically typed language does, duh). Keep that now in mind.

In JavaScript you know you must check the return value, if it is important. In short words, you cannot fool it. If the JavaScript engine says a value is a number, it is with the only exception it may be "NaH", which is simple to check for. Now let's compare it to TypeScript.

TypeScript compiler and all annotations don´t provide this essential type safety, but sure in hell they advertising like it is even it is not the case. This makes people upset. Of course, there is probably some tools that adds it on top of TypeScript, but by default TypeScript do not provide type safety because of this single reason. Maybe type safety equals no type safety.

Now let's take compare it to say Rust or C++. The compiler will enforce a strict memory layout and all CPU instructions will be very specific, so even if the developer want to return anything else but an number, they can´t. It is literally IMPOSSIBLE to return anything but an integer in a C++ program, if the declaration says it should return an integer. Do you see the difference?

Rust/C++ => Real type safety, you can trust the types.
TypeScript => No type safety, but says it has it, but you can return anything really (My example proves it).
JavaScript => No type safety, you can return anything.