Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is TypeScript really a superset of JavaScript?

I just started using TypeScript and sometimes get compiler errors "use of undeclared variable". For example the following works in plain JavaScript :

var foo = {}; foo.bar = 42; 

If I try to do the same in TypeScript it won't work and give me the mentioned error above. I have to write it like that:

var foo :any = {}; foo.bar = 42; 

In plain JavaScript the type definition with any is neither required nor valid, but in TypeScript this seems to be mandatory. I understand the error and the reason for it, but I always heard in Videos and read in the documentation:

typescriptlang.org:

"TypeScript is a typed superset of JavaScript [...]"

Introduction Video @minute 3:20:

"All JavaScript code is TypeScript code, simply copy and paste"

Is that a thing that changed during the development of TypeScript or do I have to pass a specific compiler setting to make this work?

like image 523
Markus Avatar asked Apr 28 '15 11:04

Markus


People also ask

Which statement is true TypeScript is a superset of JavaScript JavaScript is a superset of TypeScript TypeScript is a subset of JavaScript Bootstrap?

It is a superset. But this doesn't mean that it can be 1) compiled and 2) used as regular JavaScript. Objective-C is superset of C/++, but it goes with its own compiler/IDE/environment. You must follow TypeScript directives and not compare it that literally to JavaScript.

Is TypeScript a superset of ES6?

TypeScript syntax is a superset of ECMAScript 6 (ES6) syntax. The wording was changed in TypeScript 1.8 (January 2016), but the meaning is the same: TypeScript syntax is a superset of ECMAScript 2015 (ES2015) syntax. So, yes: According to the spec, TypeScript is a superset of ES6.

Is TypeScript really better than JavaScript?

TypeScript vs JavaScript: HighlightsJavaScript is better suited for small-scale applications, while TypeScript is better for larger applications. TypeScript supports static typing but JavaScript does not. TypeScript supports interfaces but JavaScript does not. TypeScript features prototyping but JavaScript does not.

Is TypeScript built on top of JavaScript?

TypeScript is a strongly typed, object-oriented, compiled programming language that builds on JavaScript. It is a superset of the JavaScript language, designed to give you better tooling at any scale.


1 Answers

The reason for TypeScript's existence is to have a compiler and language which can enforce types better than vanilla Javascript does. Any regular Javascript is valid TypeScript, syntactically. That does not mean that the compiler must be entirely happy with it. Vanilla Javascript often contains code which is problematic in terms of type security. That doesn't make it invalid TypeScript code, but it's exactly the reason why TypeScript exists and it's exactly the compiler's job to point out those problems to you.

The languages as such are still sub/supersets of one another.

like image 190
deceze Avatar answered Oct 03 '22 17:10

deceze