Is TypeScript really a superset of JavaScript?

后端 未结 3 1658
清歌不尽
清歌不尽 2020-12-25 11:29

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

var f         


        
相关标签:
3条回答
  • 2020-12-25 11:58

    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.

    0 讨论(0)
  • 2020-12-25 12:01

    Theorem: TypeScript is neither a subset nor a superset of JavaScript.

    Proof:

    When we say language A is a subset of language B, we mean all valid A-programs are also valid B-programs.

    Here is a valid TypeScript program that is not a valid JavaScript program:

    let x: number = 3;
    

    You identified a valid JavaScript program that is not a valid TypeScript program:

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

    Complicating factor 1: TypeScript is almost a superset. TypeScript is intended to be a near superset of JavaScript. Most valid JS is also valid TS. What JS is not can usually be easily tweaked to compile without errors in TS. In other words, there are many programs in the interesection of these two sets (valid TS and valid JS).

    Complicating factor 2: non-fatal errors The TypeScript compiler generates the JavaScript code you intend sometimes even if there are errors. The your example that I referenced earlier emits this error

    error TS2339: Property 'bar' does not exist on type '{}'.
    

    but also this JS code

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

    The TS documentation notes

    You can use TypeScript even if there are errors in your code. But in this case, TypeScript is warning that your code will likely not run as expected.

    I think we can call this a failed compilation (and thus invalid TypeScript) for the following reasons:

    1. The compiler seems to use the term warning in the conventional sense, so we should interpret error in the conventional sense too: an error indicates the compilation failed.
    2. The generated code could not run as expected. It could do something totally different. Imagine two languages A and B where any string that compiles in A compiles in B but does something completely differently. I would not say that A is a subset of B, even though it is syntactically.
    3. The documentation indicates that the resulting JavaScript is not necessarily correct as to what was intended. An incorrect output seems just as bad as (if not worse than) no output. They should both be considered failed.
    4. The TypeScript compiler exits with status code 2, which conventionally indicates that the process failed in some way.
    5. If we call any TypeScript program that outputs JavaScript "valid", then we would have to call the following TypeScript program valid, because it a dot compiles to the empty string after issuing errors:
    .
    

    Complicating factor 3: TS accepts JS files: The TypeScript compiler can passthrough files ending in .js (see compiler documentation for --allowJs). In this sense TypeScript is a superset of JS. All .js files can be compiled with TypeScript. This is probably not what people who visit this question are meaning to ask.

    I think complicating factor 1 is the thing that Anders Hejlsberg is getting at. It might also justify the misleading marketing on TypeScript's homepage. The other answers have fallen prey to complicating factor 2. However the general advice given in the other answers is correct: TypeScript is a layer on top of JavaScript designed to tell you when you do something bad. They are different tools for different purposes.

    0 讨论(0)
  • 2020-12-25 12:02

    The definition

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

    is true. Because any JavaScript code can passed to the TypeScript compiler.

    So it's sort of a Layer on top of JavaScript. So, of course the underlaying Layer (JavaScript) can be passed through the layers to the top (TypeScript), but not the other way around.

    Why not?

    Think of it as a bike (JavaScript) and a motorcycle (TypeScript). The basics are the same (two wheels, a frame), but the motorcycle as an engine and some enhanced features.

    So, you can use your motorcycle (TypeScript) as a bike (JavaScript), but you cannot use a bike as a motorcycle.

    EDIT:

    If your compiler throws a warning, why does it make the statement wrong? It just says: Hey, you are using TypeScript, and it's more strict than what you gave me.

    See this example, it compiles perfectly to JavaScript, but throws a warning.

    0 讨论(0)
提交回复
热议问题