DEV Community

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

Collapse
 
brunobrant profile image
Bruno Brant

Lots of strange terminology being used throughout the discussion.

There are two axis when it comes to classify languages relating to its type system:

Weak vs strong - weakly typed languages are those that allow us to cast between types forcibly. C++, for instance, allow us to cast anything to void and cast void to anything, which can lead to type mismatches.

Static vs Dynamic - static typed languages are those which the types are know at compile time, either explicitly (C++, Java, etc) or implictly (F#, etc). Dynamic typed languages means that types are determined during runtime.

Nullability Nullability is a whole other matter. Null isn't really a type, but it's a property of a variable that denotes "no value". Some static type systems incorporate Nullability better than others, like TypeScript, as some users mentioned, where you can inform the "compiler" whether or not the variable (not the type) can be null. This is similar to how mutability is treated by some languages like F#, where you need to explicitly inform that a variable can be mutated.

The point of the article is the usual one: whether annotating types is worth the effort.

First one must realize whether she believes that validating the code automatically is important. If it's enough to launch the application and test it manually, then the discussion is void.

But throughout the last decade we realized that validating our programs using automated mechanisms are a very good practice and worth the trouble.

I believe that statically typing works in the same problem domain: catch programming errors as early as possible.

If, however, you prefer to write tests with all variable combinations (and check types explicitly in code), there's no need type your code.

However, there's one additional feature that I love about typing: the better code completion/suggestions we get. This may be solved in the future when IDE get even better at type guessing, but for now they are limited when types aren't informed somehow.

Collapse
 
stereobooster profile image
stereobooster

static typed languages are those which the types are know at compile time

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.

Dynamic typed languages means that types are determined during runtime

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

Weak vs strong

Not CS terms.

Null isn't really a type

Null is a value. "Nullable" type represents set of one value.

type NullableNumber = number | null
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.