Jump to content

TypeScript type checking is really usefull?


Raktim

Recommended Posts

Okay, first I want to say you I'm very new in TypeScrpt. Now I have a question. TypeScript is a static type checking language. But, when TS code is transpile  to JS code this features got loss. Here is my code.

demo.ts
 

function add(x:number , y:number)
{
    return (x+y);
}
add(5,"Raktim");

In the above TS code x and y only capable to accept number and it will perform addition.

But, I callback this function and pass number and string intentionally to the function. So, TS compiler obviously give error as a result. But, now look the generated JS code.

demo.js
 

function add(x, y)
{
    return (x + y);
}
add(5,"Raktim");

So, in the JS code x and y are capable to accept any value. So, here this code perform string concatenation not addition. 

My question is when TypeScript type checking features got loss on JS then why we use it? 

Then, what the actual benefit behind of TypeScript type checking? Is It really useful?

Link to comment
Share on other sites

I have little experience with TypeScript, but I assume that TypeScript would have shown you an error if you had tried to call add() with a string before converting it to plain Javascript. If you are using TypeScript, you should write all of your code in TypeScript rather than converting to Javascript and then writing more code. I expect it will handle type checking as long as you are working in the TypeScript environment.

  • Like 1
Link to comment
Share on other sites

Yes, sir. But, the question is browser can only understand JS. As a result of output TS generate executable JS code. But, my question is when TS type checking features got loss in JS code then is this features really useful? Because, at the end JS will finally execute on  browser and it is dynamic type checking. 

Link to comment
Share on other sites

The TypeScript features are there to help you keep all of your types in check while you are writing your code. Once the code has been compiled, type checks don't need to be done at runtime.

Link to comment
Share on other sites

  • 2 months later...

TypeScript compiler will check the type to surface more typing errors at compiling time.

A practical example is if we use the wrong data type in the browser beacon, we now get compiling errors. Before migrating to Typescript, they could only be found by testing against the back-end.

for example:

 

var name: string;
name = 2; // type error, assign a number to a string type variable

function foo(value: number) {}
foo(''); // type error, use a number as a string type parameter

interface Bar {
    setName: (name: string) => void;
    getName: () => string;
}


var bar: Bar = {
    getName: function() {
        return 'myName';
    }
} // type error, 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...