October 2017
Intermediate to advanced
280 pages
6h 50m
English
Contextual typing occurs when the type of an expression is implied from its location; let's take this example:
document.body.addEventListener('mousedown', e => {
e.foo(); // Property "foo" does not exists on a type "MouseEvent"
}, false);
In this case, the type of the argument of the e callback is guessed by the compiler based on the context in which it is used. The compiler understands that the type of e is based on the call of addEventListener and the arguments passed to the method. In case we were using a keyboard event (keydown, for example), TypeScript would have been aware that e is of the KeyboardEvent type.
Type inference is a mechanism that allows us to write less verbose code by taking advantage of the ...
Read now
Unlock full access