I recently discovered that typescript introduces a concept called "type" to help avoid type errors during runtime.
Excited, I decided to implement this new concept in my code using VS_CODE.
Here is the snippet of code I experimented with:
//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);
Upon running the code, an error appeared 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.
Next, I examined the 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);
The resulting JS Output displayed:
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$ ;
This led me to ponder two questions:
Will typescript halt transpilation when it encounters an error?
Ultimately, since every typescript code is converted into JS code, what exactly is the purpose of typescript?