2

I have this very simple function that accept 3 integers, add them together and print the sum out to a console.

function add(n1, n2, n3) {
    var sum : number = n1 + n2 + n3;

    console.log(sum);
}

My assumption was sum is an integer therefore it enforce the type matching. So i tried this.

add(1,2,"Henok");

TypeScript does not complain, it simply print out 3Henok. Why?


Answered in the comment blow by toskv he mentioned two thing

actually what you are writing there is var sum:number = (1 as any) + (2 as any) + ('hehe3' as any) because the types of the parameters are not specified and they default to any

and to enforce compile time complaining he add this suggestion

you can enable the noImplicitAny option when compiling and tsc will complain that you have not properly typed your code.

That works for me. thank you toskv.

2
  • 1
    uhm.. how have you tried this? type checking is only enforced at compile time. at runtime it's all just javascript. Commented Oct 4, 2017 at 13:25
  • @toskv I do understand it's only a compile time checking. I put the result just to say it do pass "compile time checking" Commented Oct 4, 2017 at 13:31

2 Answers 2

2

Parameter also should specify type in typescript in order to see compilation error.

function add(n1:number, n2:number, n3:number) {
    var sum : number = n1 + n2 + n3;

    console.log(sum);
}

add(1,2, "Henok"); // compilation error

Thanks to tosky

By default parameters/variables that are not marked with any type are considered as "any" , any is literally anything (any type) and no type check is performed on any.

Don't confuse any with object, if you are using something like C# then any is similar to dynamic where type is determined at runtime.

So, expression

var sum:number = n1 + n2 + n3

is actually

var sum:number = (n1 as any) + (n2 as any) + (n3 as any)
Sign up to request clarification or add additional context in comments.

7 Comments

it's worth mentioning that by default the parameters have the type any
That i know, if you write var sum : number = 1 + 2 + "Henok";'it doesn't work (compile time error) but not inside the function. that what i do not expected to see.
actually what you are writing there is var sum:number = (1 as any) + (2 as any) + ('hehe3' as any) because the types of the parameters are not specified and they default to any
Ohh i see, I assumed the compiler worked out the correct type at compilation not at runtime.
you can enable the noImplicitAny option when compiling and tsc will complain that you have not properly typed your code. :)
|
0

If you're running typescript validation on JavaScript files, don't forget to add checkJs: true in tsconfig.json, or to add // @ts-check to the very top (i.e. before imports) of your files.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.