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?
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?
Share Improve this question edited Jul 26, 2015 at 8:45 Andrew Savinykh asked Jul 26, 2015 at 5:52 Andrew SavinykhAndrew Savinykh 26.3k20 gold badges107 silver badges164 bronze badges 9 | Show 4 more comments3 Answers
Reset to default 13It 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.
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).
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.
var i: any = 5;
once and the statement wasn’t updated as the language was? (Or maybe I’m just misremembering.) – Ry- ♦ Commented Jul 26, 2015 at 5:55Every JavaScript "program" is also a TypeScript "program"
but not necessarily a VALID TypeScript "program" – Jaromanda X Commented Jul 26, 2015 at 5:58