15

Typescript Language Specification says:

Every JavaScript program is also a TypeScript program

Now consider this code:

var i = 5;
i = "five";

This is a perfectly valid javascript that will execute with no error. It is NOT a valid TypeScript, and it will fail to compile.

There is clearly mismatch in my understanding of the quoted statement above and the code example.

Could you please clarify, what makes the spec statement true in the context of the example I gave above.

Update

To address the argument that the statement does not reflect a program validity, let's rephrase it this way:

Every JavaScript program is also a valid or invalid TypeScript program

or

Every JavaScript program is not necessarily a valid TypeScript program

If the authors wanted to say the latter, why did not they say that?

9
  • 1
    clearly the statement is flawed (or completely false, depends on your interpretation of truth) Commented Jul 26, 2015 at 5:53
  • I suppose that was equivalent to var i: any = 5; once and the statement wasn’t updated as the language was? (Or maybe I’m just misremembering.) Commented Jul 26, 2015 at 5:55
  • 1
    another way to look at it ... Every JavaScript "program" is also a TypeScript "program" but not necessarily a VALID TypeScript "program" Commented Jul 26, 2015 at 5:58
  • @JaromandaX that last interpretation does not make much sense in something as rigorous as a language spec is supposed to be. Commented Jul 26, 2015 at 5:59
  • 1
    @zespri it depends if it's a const enum or a regular enum. See this: typescriptlang.org/Playground I think the spec is missing that out, but I wouldn't consider that "a mess" Commented Jul 26, 2015 at 17:25

3 Answers 3

13

It seems the question is being asked as if the statement read:

Every JavaScript program is also a semantically correct TypeScript program

That statement would be false, but that's not what's being said here.

If you try to compile this syntactically correct TypeScript code...

var i = 5;
i = "five";

...you will get a compile error because it's not semantically correct—it's assigning a string to a variable implicitly typed as a number. However, since it's syntactically correct, the compiler will still output to the .js file with the same code above in addition to throwing the compile error.

So is every JavaScript program also a TypeScript program? Yes, but that doesn't mean you won't get compile errors.

Sidenote: You can stop the compiler from emitting on an error by specifying --noEmitOnError when compiling.


Addressing Update

What they could have done is expanded upon this by qualifying that:

Every syntactically correct JavaScript program is also a syntactically correct TypeScript program.

However, when you look at the quote in its context you can see the main idea introduced at the start of the paragraph is already about the syntax:

TypeScript is a syntactic sugar for JavaScript. TypeScript syntax is a superset of Ecmascript 5 (ES5) syntax. Every JavaScript program is also a TypeScript program.

And so, maybe the author thought saying "syntactically correct" would have been repetitious.

Sign up to request clarification or add additional context in comments.

2 Comments

This makes sense. I did not realize that it still can emits the javascript even in presence of typescript error. I'll accept an answer in a couple of days, and so far, you are the winner =)
"You can remove the condition and arrive at the original statement" exactly my point.
6

The phrase should be intended at the syntax level. The code

var i = 5;
i = "five";

is valid TypeScript syntax, but will generate a compile error because of the extra checks that TypeScript does.

Basically that part says that TypeScript is an extension and not a redesign of Javascript syntax (if this is a good thing or not really depends on your views, though).

6 Comments

Huh? This IS a syntax error in TypeScript. You do not imply that it's a semantic error (as opposed to syntax error), do you?
@zespri: it's not a syntax error. Basically the statement i = "five"; is correct... it's an expression-statement where the expression is an assignment operator call with the left side being an identifier and the right side a literal. To decide if this is valid or not there are other checks that require symbol lookup... e.g. if the identifier is defined, if it's of the correct type and so on. This kind of check is not part of what is called the language syntax. You don't get a "syntax error", you get a "type error", for example.
I respectfully disagree. The only type of errors that a compiler can detect is syntax errors. Terms "compile-time error" and "syntax error" are interchangeable. Considering a single statement in isolation is meaningless, as a single statement is not a compilation unit in TypeScript or JavaScript.
@zespri: you can use any definition you like (a definition is never wrong, by definition ;-) ). However in mainstream compiling technology (tokenization/parsing/compilation) normally a line is drawn at when symbol lookup is needed: only what comes before that line is "syntax".
The only type of errors that a compiler can detect is syntax errors -- Demonstrably false. Have a look at compilers for languages that practice strong typing. Type errors are detected at compile time in such languages.
|
1

Yes it is perfectly valid TypeScript. If this was the code in a TypeScript application it would still generate a JavaScript file for you. You can remove the warning with:

var i: string|number = 5;
i = "five";

Of course, because you have some type information hanging around, TypeScript can warn you about potential mistakes. In return, you can improve the type information it uses as I have above to explain how you intend to use a variable.

We now have an application that allows the original code, where i is a string or a number, but will prevent a later accident where someone tries to assign something else.

2 Comments

VS2015 generates error not a warning. Warning I would understand.
Until teams treat warnings more seriously, it is probably best it is an error. If this was a genuine mistake, rather than intentional code, imagine the time lost investigating the bug.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.