DEV Community

Discussion on: TypeScript is a waste of time. Change my mind.

Collapse
 
brunobrant profile image
Bruno Brant

OMFG, what the hell are you talking, son? Is this flame bait?

Right. If you open source code of almost any parser you would see that parser knows type of the value at the moment of parsing ("afsd" - string, 123 - number, etc.). By this definition JS is statically typed.

That's not it. Your examples are values, not variables. Please consider:

function foo(bar) {
   console.log(bar)
}
Enter fullscreen mode Exit fullscreen mode

What's the type of bar? It could be string, number, object, etc. The interpreter will determine it during run time and emit the code to support it accordingly, so if you call foo("abc"), foo(1), and foo({}), you get three compiled code blocks that the interpreter determined during runtime that they where required.

In case you are still not satisfied, check this code:

switch (new Date() % 3) {
   case 0: foo("abc"); break;
   case 1: foo(1); break;
   case 2: foo({}); break;
}
Enter fullscreen mode Exit fullscreen mode

If you call that code, during runtime the JS engine determines the type of the variable bar.

That's the definition of dynamic typing. Types of VARIABLES are not known/determined until runtime. That means that code can fail in runtime, not compile time, if a invalid type is provided.

If you would use reflection in Java you would be able to determine type at runtime. So Java is dynamically typed.

I may not have been clear, so: not you, the programmer. It's the compiler that doesn't know the type in compile time. Java is statically typed because all variables have type that are statically defined in compile time.

One more fundamental thing to note: variable type doesn't change in static typed languages, whereas they can change in dynamic typed languages. Check this code:

// Java/C/C#
int num = 10;
num = "abc"; // compiler error
Enter fullscreen mode Exit fullscreen mode

However, this JS code is valid:

let num = 10;
num = "abc";
Enter fullscreen mode Exit fullscreen mode

Not CS terms.

This is why I got mad at you. What makes you assert this? Take a look at the results in google:

https://www.google.com/search?sxsrf=ACYBGNQ7UX3Dc2VF-TcwYMPvKWARNQg2YA%3A1574698748299&ei=_P7bXavmEcKP0AaPw4TgCw&q=weak+vs+strong+typing&oq=Weak+vs+strong+ty&gs_l=psy-ab.3.0.0i20i263j0i22i30l9.4213.5245..6276...0.2..0.106.313.0j3......0....1..gws-wiz.......0i71j0i67j0.3V2-LF1mLSI

Not convinced?

https://www.google.com/search?tbm=bks&q=weak+vs+strong+typing

This will give you a number of results of books on compiler design that talks about the weak typing and strong typing. I hope this convinces you, for your sake.

Good luck.