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...