Or... that's what they want you to think. It is not exactly 100% JavaScript compatibile, because it throws at least 10 type errors while migrating your middle-class project to TypeScript.
The most important feature of TypeScript is... actually not types, but autocompletion in your IDE. If you install for example @types/node, you'll get autocompletion for variables like process. This is JavaScript:
import { x } from 'random-thing';
x.name = 'x';
You need to write code with documentation, because your IDE interprets types like this:
import { x } from 'random-thing';
(x as any).name = 'x';
And any is well... any object / string / number / class / function / who cares.
If you know that x will have property name, to get intelephense, you need to write comments... Yeah, comments.
JSDoc is garbage. Do you really want to write:
import { x } from 'random-thing';
// @ts-check
/** @param {{name: string}} x */
function setname(x) {
x.name = 'x';
}
setname(x);
You just need to switch to TypeScript, install required type packages and your life will get much easier. Even if there are no types, you can try:
import { x } from 'random-thing';
interface fixingtypes1 {
name: string;
}
(x as fixingtypes1).name = 'x';
This may also seem like a lot cof code, but you can reuse it, for example:
import { x } from 'random-thing';
interface fixingtypes1 {
name: string;
age: number;
}
(x as fixingtypes1).name = 'x';
(x as fixingtypes1).age = 12;
(x as fixingtypes1).name = 'xb';
(x as fixingtypes1).age = 52;
But that's rarely the case.
In most cases you can write like that:
import { x } from 'random-thing';
x.name = 'x';
The same code as in JavaScript, but you get autocompletion! Awesome.