Yes, and the reason I say that is that there are only two likely scenarios where that class in your example is going to be instantiated with an incorrect value:
- a malicious attempt to do something untoward, in which case the check isn’t really going to help because at that point the bad actor has to have direct access to the code, or
- user input, or more specifically unknown input (could be from an external API, for example)
If neither of those scenarios are in play, then the typecheck is simply there as a form of defensive programming, ie it’s being used to ensure the program works, logically. Which is what Typescript does, except TS does it before the code is compiled. This is a way of proving the logic works without running the code. So you develop using a statically typed language, which may provide a much more satisfactory level of confidence that the code will function as expected when it is actually ran.
It absolutely has, I would be extremely surprised IRL to be placed in a situation where a codebase does not use Typescript.
Obviously that is true, because types are stripped from the output code. That is not the point of using a statically typed language
On my Android phone, I can only type numbers in when I want to enter a phone number. Java is a statically typed language, yet it is not Java stopping me entering anything that isn’t a number due to the type system, it is stopping me entering them because there is some code a programmer has written (similar to your regex) that checks what the values entered into the input are.
A large % of the languages used IRL are statically typed and compiled. But when they are compiled, the output is just some bytecode or machine code instructions. All the statically typed stuff, all the semantics and syntax, that is just there as an abstraction for humans to be able to write instructions in a sane manner.
That’s what TS is. JavaScript is not statically typed, and not being statically typed has some benefits. As you say, TS is just a superset of JS and you can write everything in JS if you want. Yet TS is incredibly popular. Which should be a hint that people find the benefits of a statically typed layer on top of JS to be something that is extremely helpful.
It doesn’t magically cause your code to tell you if there is unexpected input at runtime. What you’re suggesting may seem simple and self-evident, but it isn’t at all. There are languages that can do this, that can include program state in the type system, they have been a research subject for at least 50 years. But afaik the only real existing usable implementations are either severely constrained (mathematical proof checkers like Coq, which are not turing-complete), or they involve dependent types, which are insanely complicated to work with (research languages like Idris or Agda).
Edit: is a given program correct? That is the question TS attempts to answer. And it can’t completely prove that, but it can go much further than JS can. This short talk is good:
https://www.destroyallsoftware.com/talks/ideology