InterviewSolution
Saved Bookmarks
| 1. |
What is meant by contextual typing? |
|
Answer» When the TypeScript compiler USES the location (or context) of a variable to infer its type, it’s called CONTEXTUAL typing. In the following example, TypeScript uses the Window.onmousedown function type information to infer the type of the function expression on the right-hand side of the assignment. This allows it to infer the type of the e parameter, which does have a button property but not a property NAMED foo. window.onmousedown = function (e) {console.log(e.button); //<- OKconsole.log(e.foo); //<- ERROR!}; |
|