Typescript: Documentation is wrong about TypeScript being superset of Javascript?

Created on 28 Apr 2020  ·  5Comments  ·  Source: microsoft/TypeScript

Here is a slogan on https://www.typescriptlang.org/ which says _TypeScript is a typed superset of JavaScript that compiles to plain JavaScript._ That means that any valid JS code is also a valid ts code. So I can just rename any .js file into .ts file, possibly mix it with some TS constucts and it should work (possibly, with some compiler options enabled or disabled). Although, here is an example of code that I couldn't make work:

Code

var foo = {};
foo.bar = 42;
window["A"] = 10;
alert(A);

Expected behavior:
Compiles into

var foo = {};
foo.bar = 42;
window["A"] = 10;
alert(A);

without errors
Actual behavior:
Multiple compilation errors

Duplicate

Most helpful comment

Duplicate of https://github.com/microsoft/TypeScript/issues/37908. Some useful comments in there from members of the Typescript team.

All 5 comments

Spontaneously...

var foo: {bar?: number} = {};
foo.bar = 42;
(window as any)["A"] = 10;
declare var A: number;
alert(A);

edit: add declare, still not executed :D

TypeScript is a typed superset of JavaScript that compiles to plain JavaScript

This means that all JavaScript syntax and features are supported in TypeScript. It doesn't mean that all JavaScript is valid TypeScript - such a meaning would be ad absurdum, it would invalidate the entire point of TypeScript: additional type safety.

This means that all JavaScript syntax and features are supported in TypeScript. It doesn't mean that all JavaScript is valid TypeScript - such a meaning would be ad absurdum

I always thought that is exactly what "superset" means.

it would invalidate the entire point of TypeScript: additional type safety.

No, it would make sense in at least situation when you have a "bad" TS file and enhance it, gradually enforcing more and more strict rules. IIRC that was recommende for migration from JS to TS in 0.x-1.x versions of TypeScript.

Duplicate of https://github.com/microsoft/TypeScript/issues/37908. Some useful comments in there from members of the Typescript team.

I'm still pretty sure #37908 was closed by mistake. I mean Scala has all the features Java have it doesn't make Scala superset of Java.

There are suggestions how to make migration easier but there is nothing about why it should be considered a superset.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MartynasZilinskas picture MartynasZilinskas  ·  3Comments

zhuravlikjb picture zhuravlikjb  ·  3Comments

dlaberge picture dlaberge  ·  3Comments

fwanicka picture fwanicka  ·  3Comments

kyasbal-1994 picture kyasbal-1994  ·  3Comments