- Published on
TypeScript is *technically* not a superset of JavaScript
- Authors

- Name
- Timothy Herchen
Wikipedia claims:
As TypeScript is simply a superset of JavaScript, existing JavaScript can be adapted to TypeScript and TypeScript program can seamlessly consume JavaScript.
Quick fact check. Some years ago, when I was porting an old JavaScript project to TypeScript, I wrote code along these lines:
function f(a, b) {
const c = 1 << a
const d = a >> (b - 1)
// ...
}
Unfortunately, the TypeScript compiler considered—and still considers—the < and > to be delimiting generic arguments. After reporting a cascade of syntax errors, tsc outputs:
function f(a, b) {
const c = 1(b - 1);
}
Oh noes! 🤭
This got me and my friend Sean thinking about other cases where syntactically valid JavaScript gets owned by the TypeScript compiler. He quickly found the simpler case:
let a = 0;
let array = [];
console.log(a < 0, a > (array.length - 1));
let a = 0;
let array = [];
console.log(a(array.length - 1));
with a similar root cause. Sean's example is particularly enjoyable because, unlike the original example, it not only doesn't involve the oft-maligned automatic semicolon insertion (ASI), but also doesn't evoke any syntax errors from tsc. If you renamed the .js file to .ts and added // @ts-nocheck (say, during an incremental migration to TS), you'd be none the wiser until it crashed at runtime!
Based on my perusal of the TypeScript issue tracker, this bug class (erasing <...> before a "function call") seems to be the only commonly encountered incompatibility of this type.