There are two declaration spaces in TypeScript: the variable declaration space and the type declaration space. These concepts are explored below.
Type Declaration Space
The type declaration space contains stuff that can be used as a type annotation. E.g. the following are a few type declarations:
classFoo{};interfaceBar{};typeBas={};
This means that you can use Foo, Bar, Bas, etc. as a type annotation. E.g.:
varfoo:Foo;varbar:Bar;varbas:Bas;
Notice that even though you have interface Bar, you can't use it as a variable because it doesn't contribute to the variable declaration space. This is shown below:
interfaceBar{};varbar=Bar;// ERROR: "cannot find name 'Bar'"
The reason why it says cannot find name is because the name Baris not defined in the variable declaration space. That brings us to the next topic "Variable Declaration Space".
Variable Declaration Space
The variable declaration space contains stuff that you can use as a variable. We saw that having class Foo contributes a type Foo to the type declaration space. Guess what? it also contributes a variableFoo to the variable declaration space as shown below:
classFoo{};varsomeVar=Foo;varsomeOtherVar=123;
This is great as sometimes you want to pass classes around as variables. Remember that:
we couldn't use something like an interface that is only in the type declaration space as a variable.
Similarly something that you declare with var, is only in the variable declaration space and cannot be used as a type annotation:
The reason why it says cannot find name is because the name foois not defined in the type declaration space.