You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Item 80: Use @ts-check and JSDoc to Experiment with TypeScript
Things to Remember
Add "// @ts-check" to the top of a JavaScript file to enable type checking without converting to TypeScript.
Recognize common errors. Know how to declare globals and add type declarations for third-party libraries.
Use JSDoc annotations for type assertions and better type inference.
Don't spend too much time getting your code perfectly typed with JSDoc. Remember that the goal is to convert to .ts!
Code Samples
// @ts-checkconstperson={first: 'Grace',last: 'Hopper'};2*person.first// ~~~~~~~~~~~~ The right-hand side of an arithmetic operation must be of type// 'any', 'number', 'bigint' or an enum type
// @ts-check/** * Gets the size (in pixels) of an element. * @param {Node} el The element * @return {{w: number, h: number}} The size */functiongetSize(el){constbounds=el.getBoundingClientRect();// ~~~~~~~~~~~~~~~~~~~~~// Property 'getBoundingClientRect' does not exist on type 'Node'return{width: bounds.width,height: bounds.height};// ~~~~~ Type '{ width: any; height: any; }' is not// assignable to type '{ w: number; h: number; }'}