27

Currently I've started to learn TypeScript. From the documents I've studied for TypeScript, I saw some samples that pure JavaScript code could be compiled as TypeScript code.

My question is: Is TypeScript language designed in a way that any JavaScript code will be a valid TypeScript code?

i.e. is any .js file a valid .ts file?

ngood97
  • 513
  • 5
  • 16
mehrandvd
  • 8,806
  • 12
  • 64
  • 111

3 Answers3

21

Let's assume valid code means : is syntactically correct with respect to the language specifications.

Then the answer is YES.


It is written down in the TypeScript Specifications (second paragraph) :

TypeScript is a syntactic sugar for JavaScript. TypeScript syntax is a superset of ECMAScript 2015 (ES2015)syntax. Every JavaScript program is also a TypeScript program.

(emphasis mine)

Now, most often you don't want basic JavaScript to be used "uncontrolled". After all, that was one of the reasons to create the TypeScript language in the first place !

Nevertheless, a valid JavaScript program is technically valid TypeScript. This is in the specifications probably by need of "backward compatibility", or, better formulated, by need of supersedence to ECMAScript


To take the example of another answer, the Typescript code

var testVar = 4;
testVar = "asdf";

will be transpiled into exactly the same JavaScript code (with all default compiler options)

Demonstration here on Typescriptlang.org playground

even though there is a TypeScript error, this doesn't prevent a valid javascript to be output from there. It "compiles with error". (I wish this was called a warning instead of error, but anyway).


See also : https://basarat.gitbooks.io/typescript/content/docs/why-typescript.html

Your JavaScript is TypeScript

TypeScript provides compile time type safety for your JavaScript code. This is no surprise given its name. The great thing is that the types are completely optional. Your JavaScript code .js file can be renamed to a .ts file and TypeScript will still give you back valid .js equivalent to the original JavaScript file. TypeScript is intentionally and strictly a superset of JavaScript with optional Type checking.

The simplest option that can deactivate this behavior (output js even though there are Type errors) is --noEmitOnError

Pac0
  • 21,465
  • 8
  • 65
  • 74
  • 1
    As you can see in the playground, it's not valid typescript (see the red line under `testVar` – FINDarkside Nov 14 '18 at 13:29
  • 1
    @FINDarkside This is an error in TypeScript, but nevertheless **it is still technically valid TypeScript, and the compiler outputs javascript from this, which happens to be exactly the same code**. I just tried it again now, with the exact same code than my link in the playground. I used `tsc myfile.ts`, without any tsconfig.json. I got the same error, but I also got a functional js file. As per the **specifications**. It is by design that it works like that. – Pac0 Nov 14 '18 at 13:46
  • @FINDarkside Please also check https://basarat.gitbooks.io/typescript/content/docs/why-typescript.html : ***Your JavaScript is TypeScript** TypeScript provides compile time type safety for your JavaScript code. This is no surprise given its name. The great thing is that the types are completely optional. Your JavaScript code .js file can be renamed to a .ts file and TypeScript will still give you back valid .js equivalent to the original JavaScript file. TypeScript is intentionally and strictly a superset of JavaScript with optional Type checking.* – Pac0 Nov 14 '18 at 13:47
  • and just for the record, I tried this with tsc 3.1.3, but it should be the same for any version. – Pac0 Nov 14 '18 at 13:51
  • If your "compiles" means "compiles with errors", you should clarify your answer. It's pretty obvious that's not what you meant originally, because you said that `noImplicitAny` could make it not to compile, which by your definition of compile is false. – FINDarkside Nov 14 '18 at 13:51
  • 1
    @FINDarkside fair point, indeed I added an edit to my question, mentionning the "`noEmitOnError`" wihch is more likely to be the correct option for my point. I'm going to remove the mention of the `noImplicitAny`. – Pac0 Nov 14 '18 at 13:52
13

Not any valid JavaScript code is valid TypeScript see my example below.

var testVar = 4;
testVar = "asdf";

TypeScript gives the following error: Cannot convert string to number. To make that work in TypeScript add ":any" like below.

var testVar: any = 4;
testVar = "asdf"

This happens because TypeScript noticed testVar is declared and in the declaration it is assigned a number and therefore it decides it should stay a number.

Dick van den Brink
  • 13,690
  • 3
  • 32
  • 43
  • 2
    another example `foo = 123` invalid in typescript as `foo` is never declared i.e. no `var` PS this code would fail in strict mode javascript as well. – basarat Dec 25 '13 at 12:31
  • 1
    for me the above Javascript code works in TypeScript file...Any suggestions? – Marek Feb 05 '16 at 11:19
  • 4
    This is true only if the Typescript compiler option `noImplicitAny` is `true`. With it being `false`, which is the default case, that is valid Typescript. – Tuupertunut Oct 14 '16 at 19:21
  • 1
    @Tuupertunut No, this is not valid TypeScript and I doubt it ever was. The error is that testVar has type `Number`, so this has nothing to do with `noImplicitAny`. I actually tried this with 3.1.6, 1.7.5 and even with the first available version on npm, 0.8.0 and none of them compiled the code above. – FINDarkside Nov 14 '18 at 13:33
  • @FINDarkside Did you check the compiled js file? I just now tried, and though there is error, js file is generated. – Dzenly Dec 04 '18 at 03:39
  • @Dzenly Yes typescript will output a js file, but saying that it's valid typescript when it compiles with errors is misleading. By the same logic even `const a: string = 2;` is valid typescript. See the comments of the accepted answer. – FINDarkside Dec 04 '18 at 14:15
0

As per TypeScript Specifications : Archived doc here

The statement "Every JavaScript program is also a TypeScript program."

Implictely states below.

"Every JavaScript program is also a TypeScript program with necessary/required changes in program such that it does not show any complication errors/warnings by TypeScript Complier"

Chaitanya Babar
  • 269
  • 3
  • 12