tmtk75 tmtk75 - 7 months ago 40
TypeScript Question

Why does TypeScript allow to compile?

There are two simple TypeScript codes.





a.ts can be compiled, but compiler complains for b.ts

$ ./node_modules/.bin/tsc a.ts
$ ./node_modules/.bin/tsc b.ts
b.ts(1,13): error TS2304: Cannot find name 'a'.

Why can a.ts be succeeded?
I'm really confused.

I'm using v2.0.10

$ ./node_modules/.bin/tsc --version
Version 2.0.10



If you execute the following code in Chrome developer tools

for(var b in window) { 
  if(window.hasOwnProperty(b)) console.log(b); 

You'll see a property "name" (in the case of a Web Browser, the global scope is the window).

So the TypeScript compiler allows you to reference 'name' (what would otherwise be an undeclared variable) because it knows that it exists on window (the global scope). Whereas there is no property 'a' in the global scope, so the compiler is doing its job and telling you about it.