TypeScript's secret parallel universe

Click for: original source

Florian Reuschel put together this tutorial for new TypeScript users. He focuses on one of the most underreported topics in the TypeScript tutorial world: the type scope.

The author centers this article around how he approached solving problem when he was writing a library with a bunch of classes distributed over various folders. For the library’s public API, I wanted those classes to be exposed as a single nested object (e.g. the Console class from Output/Console.ts being available as API.Output.Console).

TypeScript has a secret scope. TypeScript basically maintains a type scope which is completely independent of the variable scope of JavaScript. This means that you may declare a variable foo and a type foo in the same file. 🤯 They don’t even need to be compatible.

Now classes in TypeScript are a little bit special. What happens if you define a class Foo is that TypeScript not only creates a variable Foo (containing the class object itself) it also declares a type Foo, representing an instance of the Foo class.

Read more, to get the rest of infor from this excellent article!

[Read More]

Tags javascript web-development nodejs