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.