let message: string = 'Hello World';
console.log(message);

QnA about on Data Type

1. Question:
We know that TypeScript (TS) converts into JavaScript (JS). If I declare types in TypeScript, do those types remain in the TypeScript code after it is converted into JavaScript?

Answer:
No, TypeScript types do not remain in the JavaScript code after compilation. TypeScript’s type system is only used for type checking during development, and the types are removed when the code is compiled into JavaScript. The resulting JavaScript is dynamically typed.

2. Question:
Then why is TypeScript called strongly typed?

Answer:
TypeScript is called “strongly typed” because it enforces type checking during development.

  • In TypeScript, you declare types for variables, function parameters, and return values, and the compiler checks if the types are correct before converting the code to JavaScript.
  • This static type checking helps catch errors at compile time, preventing type-related bugs.

However, once TypeScript is compiled to JavaScript, the types are removed, and JavaScript is dynamically typed. The “strongly typed” aspect refers to TypeScript’s type-checking at development time, not the behavior of the compiled JavaScript.