3 ms·
There's a big misconception that JS has zero typechecking. This is not true; rather, JS has dynamic typechecking. It turns out that when you're making an API fo
by hajile 11y ago
There's a big misconception that JS has zero typechecking. This is not true; rather, JS has dynamic typechecking. It turns out that when you're making an API for an external call, you will need to manually check the types of the incoming data anyway (and this is where I see 99% of JS type errors). This is something that JS is equipped to do fairly easily when necessary.
A typical type problem would be sending a float instead of an integer. Sending a float instead of a regex would not be a typical problem.Static types are necessary in less dynamic languages because they have multiple competing types which can't be distinguished otherwise (eg. all the integer subtypes) and they have structs/classes that cannot be dynamically modified. JS types are simple (number, object, string, etc) and very different in nature. In addition, this kind of error will be caught quite quickly with even the most rudimentary unit tests. Half-way decent docs where modules interact should solve the remaining issues. Running into type errors within the same file or two is probably symptomatic of a lack of composition.
Enter Typescript. It has one major positive in that it prevents some dynamic techniques that can tank performance (though performance graphing and reducing dynamism is probably a better solution). Because you cannot depend on everyone using typescript, all external APIs must STILL have dynamic type checking.
This leaves you with type checking within your own project. Typescript can be turned off anywhere a programmer wants making it unable to guarantee type safety. It adds a lot of extra typing and visual overhead. Unlike C# (where it gets it's type syntax inspiration), the types do nothing for the JIT because the JIT never sees them.
There's no reason for a modern type system to not be based on Hinley-Milner. There's no reason for 95% of typing to be inferred by the computer rather than forcing it to be typed out. There's no reason to use a system that's only good for the assembler when the assembler will never even see it.