### Installing Mitt via npm Source: https://github.com/developit/mitt/blob/main/README.md This command demonstrates how to install the Mitt library as a project dependency using the npm package manager. The '--save' flag adds it to the project's package.json file. ```shell npm install --save mitt ``` -------------------------------- ### Basic Mitt Usage in JavaScript Source: https://github.com/developit/mitt/blob/main/README.md This example demonstrates the core functionality of Mitt: creating an emitter instance, attaching event listeners ('on') for specific types and a wildcard '*', emitting events ('emit') with data, clearing all handlers, and detaching specific handlers ('off') using references. ```javascript import mitt from 'mitt' const emitter = mitt() // listen to an event emitter.on('foo', e => console.log('foo', e) ) // listen to all events emitter.on('*', (type, e) => console.log(type, e) ) // fire an event emitter.emit('foo', { a: 'b' }) // clearing all events emitter.all.clear() // working with handler references: function onFoo() {} emitter.on('foo', onFoo) // listen emitter.off('foo', onFoo) // unlisten ``` -------------------------------- ### Type Safety with Mitt in TypeScript (Explicit Type) Source: https://github.com/developit/mitt/blob/main/README.md This TypeScript example demonstrates explicitly using the 'Emitter' type provided by Mitt. This provides the same type safety benefits as type inference but explicitly declares the type of the emitter variable. ```typescript import mitt, { Emitter } from 'mitt'; type Events = { foo: string; bar?: number; }; const emitter: Emitter = mitt(); ``` -------------------------------- ### Type Safety with Mitt in TypeScript (Inference) Source: https://github.com/developit/mitt/blob/main/README.md This TypeScript example shows how to use Mitt with type definitions for events. By providing a type argument to 'mitt()', TypeScript can infer the type of the event payload ('e') in listeners and catch type mismatches when emitting events, especially with 'strict' mode enabled. ```typescript import mitt from 'mitt'; type Events = { foo: string; bar?: number; }; const emitter = mitt(); // inferred as Emitter emitter.on('foo', (e) => {}); // 'e' has inferred type 'string' emitter.emit('foo', 42); // Error: Argument of type 'number' is not assignable to parameter of type 'string'. (2345) ``` -------------------------------- ### Including Mitt via CDN Source: https://github.com/developit/mitt/blob/main/README.md This HTML snippet shows how to include the Mitt library directly in a web page using its UMD build hosted on the unpkg CDN. The library will be available globally as 'window.mitt'. ```html ``` -------------------------------- ### Importing Mitt in JavaScript Source: https://github.com/developit/mitt/blob/main/README.md This snippet shows how to import the Mitt library into a JavaScript project using both ES6 module syntax (for bundlers like Webpack/Rollup) and CommonJS syntax (for Node.js environments). ```javascript // using ES6 modules import mitt from 'mitt' // using CommonJS modules var mitt = require('mitt') ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.