最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Is every JavaScript program also a TypeScript program? - Stack Overflow

programmeradmin1浏览0评论

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
  • 1 clearly the statement is flawed (or completely false, depends on your interpretation of truth) – Jaromanda X 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.) – Ry- 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" – Jaromanda X 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. – Andrew Savinykh Commented Jul 26, 2015 at 5:59
  • 1 @zespri it depends if it's a const enum or a regular enum. See this: goo.gl/R0GQLe I think the spec is missing that out, but I wouldn't consider that "a mess" – David Sherret Commented Jul 26, 2015 at 17:25
 |  Show 4 more comments

3 Answers 3

Reset to default 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.

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.

发布评论

评论列表(0)

  1. 暂无评论