Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

is typescript truly strict?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

"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?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading