Functions
Specialized Parameters
The JavaScript runtime does not have runtime support for function overloading. There can be only a single function body for any given name in scope. However people do support function overloading by utilizing the highly dynamic nature of JavaScript e.g. a getter and a setter:
Such implementation can be captured by the TypeScript’s type system by providing function signatures before the function implementation:
Note that when you define function overloads this way, the last signature is actually not callable. You have to provide it however to help the implementer of the function be aware of the consequences of his overload signatures. For example in the following example the function with the signature function callMe(v1?: any, v2?: any): any
is not open to public use:
Interfaces
Interfaces have a lot of power in TypeScript. This is because they are designed to capture all the complexity of
Ambient Declarations
We previously had a brief look at ambient declarations in the section why typescript?. One of the core design goals of TypeScript is to allow easy consumption of existing JavaScript libraries. You can declare the type information for existing JavaScript using ambient declarations. You declare ambient stuff using the keyword. In fact this is how a bunch of stuff available by default in a browser environment (e.g window
, etc) is declared in a file called lib.d.ts
Note: You can find type definitions for nearly 90% of the most popular JavaScript libraries at with contributions from lots of developers.
Interfaces
Type Alias
Type Inference
It tries to infer as much as it can so that you don’t need to explicitly type your code.
Specialized
Type Assertion
If A is a subtype of B or B is a subtype of A.