0

It is claimed that TypeScript is a superset of Javascript. Here's a question on Stack about this. Here's a quote from spec:

TypeScript is a syntactic sugar for JavaScript. TypeScript syntax is a superset of ECMAScript 2015 (ES2015) syntax. Every JavaScript program is also a TypeScript program.

So my understanding is that any stand-alone javascript file can be treated as a valid typescript code, i.e. compiled (may be with some additional flags) by tsc compiler.

But here's an example of js code:

class ClassA {}
ClassA.prototype.ping = () => {console.log('PING')}

That is valid javascript but if you'll try to compile it with typescript, you'll get: error TS2339: Property 'ping' does not exist on type 'ClassA'

One can declare interface which ClassA can implement, also, it's highly untypical to write code like this (combine class and prototype syntaxes) but nevertheless - this looks like an example of valid js code which raises an error while compiling with tsc.

So the question is - how this does not contradict to the quote from spec?

Community
  • 1
  • 1
shabunc
  • 23,119
  • 19
  • 77
  • 102
  • it might be valid JS, but it's not good JS. If you're using classes, don't do prototype manipulation. Either write true constructor function + prototype code, or use proper class code. Pretty sure typescript's preventing you from doing *really weird shit(tm)* here. `class ClassA { ping() { console.log('PING'); }, ... }` – Mike 'Pomax' Kamermans Oct 24 '16 at 16:15
  • For future reference "Why does TS throw an error on this code?" might have been a more straightforward way of asking this same question, without the weirdly accusatory tone. – Retsam Oct 24 '16 at 16:21
  • @Retsam sure, let me edit title (at least) to get rid of any accusatory annotations, it's not about any accusations. – shabunc Oct 24 '16 at 16:28
  • @Mike'Pomax'Kamermans it definitely non-idiomatic but it's still a valid js code, and when something is claimed to be a superset one can expect this be executed. – shabunc Oct 24 '16 at 16:48
  • 1
    not necessarily: while the *syntax* is considered a superset, that does not mean *parsing* is identical. Compilers are allowed to be opinionated (although within that, there is always room for bugs or incompletions) – Mike 'Pomax' Kamermans Oct 24 '16 at 16:53
  • See [What does "all legal JavaScript is legal TypeScript" mean?](https://stackoverflow.com/questions/41750390/what-does-all-legal-javascript-is-legal-typescript-mean) – Bergi Aug 18 '23 at 22:56

1 Answers1

5

TypeScript is a syntactic superset of JavaScript that doesn't change JavaScript's runtime behavior. So any expression or statement or declaration you write in JavaScript is syntactically legal TypeScript.

This doesn't mean that all JS code is considered to be warning-free TypeScript. After all, a major goal of TypeScript is to identify bad constructs like

var s = "hello world" * 423;
var t = "qzz".subtr(2);
var u = [1, 2, 3] + 5;
var w = window.navgtator;

These are all "valid" JavaScript expressions, they just happen to have undesirable runtime behavior that people don't want to actually do.

As usual in TypeScript, you can tell the type system about extra information

class ClassA {}
// Declare additional method
interface ClassA { ping(): void; }
// OK
ClassA.prototype.ping = () => {console.log('PING')}
Ryan Cavanaugh
  • 209,514
  • 56
  • 272
  • 235
  • 1
    A [discussion](https://github.com/Microsoft/TypeScript/issues/6373) can be found here, same guy I guess commented at the end :) – sabithpocker Oct 24 '16 at 16:17
  • 1
    well, but strictly speaking, it's not a warning, it's a compile-time error. I'm not arguing, just saying. – shabunc Oct 24 '16 at 16:50
  • 1
    TS doesn't have a notion of warning vs error; I say "warning" here because TS will still emit code (unless you have noEmitOnErrors turned on). – Ryan Cavanaugh Oct 24 '16 at 16:55
  • oh that's just great, thank you for clarification! - I've totally missed the point that code is still generated! – shabunc Oct 24 '16 at 17:24