Jump to content
Sign in to follow this  
Raktim

TypeScript type checking is really usefull?

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?

Share this post


Link to post
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

Share this post


Link to post
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. 

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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, 

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...