Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is every JavaScript program also a TypeScript program?

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?

like image 338
Andrew Savinykh Avatar asked Jul 26 '15 05:07

Andrew Savinykh


People also ask

Is all JavaScript TypeScript?

Every JavaScript program is also a TypeScript program. After all, that was one of the reasons to create the TypeScript language in the first place !

Is JavaScript the same as TypeScript?

In a nutshell, TypeScript is an object-oriented programming language, whereas JavaScript is a scripting language. Thus, TypeScript offers interfaces and modules through ES6 features; on the other hand, JavaScript doesn't offer such features.

Is JavaScript part of TypeScript?

TypeScript is nothing but JavaScript and some additional features i.e. ES6 features. It may not be supported in your target browser but the TypeScript compiler can compile the . ts files into ES3, ES4, and ES5 also.

Why do we need TypeScript instead of JavaScript?

In terms of software development, TypeScript offers many advantages over JavaScript: Optional static typing. JavaScript is a dynamically typed language, which means that types are checked, and data type errors are only detected at runtime. This can be very dangerous and can create errors during production.


1 Answers

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.

like image 176
David Sherret Avatar answered Oct 24 '22 07:10

David Sherret