Lit Analyzer is a command-line interface (CLI) tool designed to provide static analysis and type checking for Lit templates within JavaScript and TypeScript projects. It enhances the developer experience by catching common errors related to Lit element properties, events, and slots before runtime. The current stable version is 2.0.3, with regular updates to support new Lit features and address community feedback. Key differentiators include its deep integration with TypeScript for template-bound expressions, offering autocompletion suggestions, and enforcing best practices through configurable rules, such as ensuring proper property visibility (`@property`, `@internalProperty`) and correct custom element registration in `HTMLElementTagNameMap`. Its development is active, with significant improvements introduced in the transition to v2.
npm install lit-analyzerNo compatibility data collected yet for this library.
This quickstart demonstrates how to install and run `lit-analyzer` on a basic Lit component to catch common template-related type errors and best practice violations.
Review `lit-analyzer`'s output carefully after upgrading. Address any new warnings or errors by adjusting component properties, event listeners, or template expressions to comply with stricter type definitions or new rule enforcements.
Ensure a valid `tsconfig.json` exists in your project. If your Lit files are not directly within the `include` paths, adjust the `tsconfig.json` or specify `files` explicitly. Example: `{ "compilerOptions": { "target": "es2020", "module": "esnext" }, "include": ["src/**/*.ts", "./my-element.ts"] }`.Add a `declare global` block to one of your TypeScript definition files (e.g., `global.d.ts` or directly in the component file) to extend `HTMLElementTagNameMap` with your custom element. Example: `declare global { interface HTMLElementTagNameMap { 'my-element': MyElement; } }`.Explicitly decorate public properties that should be exposed with `@property()`. Ensure internal-only properties either use `@internalProperty()` or are not decorated with `@property()` if they are not meant to be reactive or exposed to the outside.
Create a `tsconfig.json` file in your project root. A minimal configuration will suffice for `lit-analyzer` to operate, e.g., `{ "compilerOptions": { "target": "es2020", "module": "esnext", "moduleResolution": "node" }, "include": ["src/**/*.ts"] }`.Define the property explicitly on your LitElement class, ensuring it has the correct name and type, and is potentially decorated with `@property()` if it's a reactive property. Double-check for typos.
Add a `declare global` block in a TypeScript file to extend `HTMLElementTagNameMap` with your custom element's tag and class type: `declare global { interface HTMLElementTagNameMap { 'my-component': MyComponentClass; } }`.Ensure that the event name is a valid DOM event (e.g., `click`, `input`) or a correctly dispatched custom event from the component. If it's a custom event, ensure the component itself dispatches it correctly and consider adding JSDoc `@fires` tags for better documentation and tooling.
No dependency data recorded yet.