I tried to clear my doubts regarding TypeScript, but not able to clarify it well, what I understand, TypeScript is a strict syntactical superset of JavaScript, which makes our code a lot better, clean, and meaningfully as we can use different types to define our object, variables, and classes.
however, what happens if we are using JavaScript code, which got converted from the TypeScript code, to perform a certain task? as far as I understand, it does not guarantee for type restriction anymore.
so couple of pointers here which I understood (maybe I'm wrong or not understood correctly):
- TypeScript only allows us to code better.
- Does not gives the guarantee after conversion.
- It gives us a
.d.tsdeclaration file which we can use in any other TypeScript project. (it's useless in JS?)
Please see the below example:
TypeScript Code:
// Takes number only
const log = (a: number) => {
console.log(a);
}
// NOTE: Passing string in ts showing/highlighting the issue,
// however after conversion from TS to JS,
// we can pass a string to the log method.
log('a');
Got Converted Into JavaScript Code as below:
"use strict";
// Takes number only
const log = (a) => {
console.log(a);
};
// NOTE: Passing string in ts showing/highlighting the issue,
// however after conversion from TS to JS,
// we can pass a string to the log method.
log('a');
If anyone knows, can you please explain if this is a valid understanding?
If yes, I was planning to work on some small library, which will help us to assert the object, classes, or variables regardless of TypeScript or JavaScript, when I thought to work on this, I realized we already have TypeScript.
Please excuse my typos.
Thanks.