-1

I read that typescript introduces the concept of assigning the variable "a type" so that we can avoid type errors at runtime.

I tried implementing the "type" concept in VS_CODE.

This is the code i tried

//main.ts

let message = "Hello all of you!!"

console.log(message);

let message2 : string = "Welcome typescript with the concept of 
'type' ";

console.log(message2);

message2 = 233;

console.log(message2);

This is the error i got in the console.

main.ts:9:1 - error TS2322: Type '233' is not assignable to type 
'string'.

9 message2 = 233;

[00:30:56] Found 1 error. Watching for file changes.

Transpiled JS Code

//main.js
"use strict";
var message = "Hello all of you!!";
console.log(message);
var message2 = "Welcome typescript with the concept of 'type' ";
console.log(message2);
message2 = 233;
console.log(message2);

JS Output

venkat-6805:Typescript-basics venkat-6805$ node main
Hello all of you!!
Welcome typescript with the concept of 'type' 
venkat-6805:Typescript-basics venkat-6805$ node main
Hello all of you!!
Welcome typescript with the concept of 'type' 
233
venkat-6805:Typescript-basics venkat-6805$ ;

So my question is

  1. Won't typescript stop transpilation when it finds an error?

  2. Eventually, every typescript code is converted into JS code. Then what is the use of typescript?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437

1 Answers1

1

1) Won't typescript stop transpilation when it finds an error

If you want to prevent Typescript from transpiling when there's such an error, you should set noEmitOnError to true, in your tsconfig.json, eg:

{
  "compilerOptions": {
    "noEmitOnError": true
  }
}

Otherwise, typing errors will not prevent the code from being transpiled.

2) Eventually, every typescript code is converted into JS code. Then what is the use of typescript

Because it's relatively easy to fix compile-time errors, but it can be much more difficult to fix runtime errors. For a particularly bad case, consider a section of code which only gets run rarely, and you forget to cast a string to a number before using + on it. So, instead of someVariable + 5 resulting in a number which gets 5 added to it, it results in a string which gets 5 concatenated to it, which is unexpected, and may eventually cause a bug later down the line. If this eventually results in an error, the ultimate source of the error may be difficult to track down. This could waste a lot of programmer's time in a large codebase, especially if the person who wrote the original code isn't the one debugging.

With Typescript, this rare runtime error is turned into a compile-time error, when you try to use a string where a number is required. This is an easy 5-second fix, which is quite an improvement.

Of course, there are other opinion-based advantages and disadvantages of Typescript (it takes some time to learn, and everyone working on the code needs to know it to use it), but this is one sort of advantage to keep in mind.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320