Home > Enterprise >  is typescript truly strict?
is typescript truly strict?

Time:05-04

I am new to typescript so this may be a dumb question or a odd question, which I don't doubt that it is, but please look at this example of what I mean:

typescript

var num:number = 5
console.log(num)

compiled to js:

"use strict";
var num = 5;
console.log(num);

in the compiled code there is not anything really restricting num from being anything but a number. So, is typescript really strict? Or is it just creating the allusion of being strict? Or perhaps is there a option in the tsconfig.json that is suppose to make the code strict that I missed?

CodePudding user response:

TypeScript tries to guarantee types at compile time, at runtime everything is dynamic. So if you communicate with an API and type it using TypeScript but the API delivers something else, then the "illusion", as you called it, will break.

  • Related