TypeScript Definitions for Chrome Extensions
The `chrome-types` package provides official TypeScript declaration files for Chrome Extensions, automatically generated from the Chromium source code. This ensures the definitions are highly accurate and consistently up-to-date with the latest Chrome API changes. Published daily via GitHub Actions, the package reflects Chrome's HEAD revision, making it the most current source for Chrome extension types. It is currently at version 0.1.425. The default `index.d.ts` file includes only Manifest V3 (MV3) and newer types, while a separate `_all.d.ts` file is available for projects requiring definitions for deprecated Platform Apps APIs. This package is crucial for developers building Chrome extensions with TypeScript, providing robust type safety and autocompletion for the global `chrome` object and its various namespaces, distinguishing itself from community-maintained alternatives like `@types/chrome` by its direct origin from the Chromium project itself.
Common errors
-
Cannot find name 'chrome'.
cause The TypeScript compiler is not aware of the global `chrome` object types.fixEnsure `chrome-types` is installed (`npm install --save-dev chrome-types`) and correctly configured in your `tsconfig.json` under `compilerOptions.types` or `compilerOptions.typeRoots`. -
Property 'tabs' does not exist on type 'typeof chrome'. Did you mean 'tabCapture'?
cause Attempting to use a Chrome API (like `chrome.tabs`) that is deprecated or not available in the Manifest V3 context by default, while only the default MV3+ types are loaded.fixVerify that the API you are using is compatible with Manifest V3. If you intend to use deprecated APIs, ensure you are loading the `_all.d.ts` definition file: `/// <reference types="chrome-types/lib/_all" />`. -
Type 'X' is not assignable to type 'Y'.
cause Strict type checking or changes in Chrome API signatures causing type mismatches.fixReview the specific type error message. Consult the official Chrome Extensions documentation for the correct API signature. Update your code to match the expected types, or consider narrowing types if the API has become more specific.
Warnings
- gotcha By default, `chrome-types` provides definitions tailored for Manifest V3 (MV3) and newer extensions, located in `index.d.ts`. Older or deprecated Platform Apps APIs are excluded from this default.
- gotcha `chrome-types` is an official, auto-generated package, which usually keeps it more up-to-date and accurate with Chromium's latest changes compared to the community-maintained `@types/chrome`. Using both simultaneously can lead to type conflicts.
- gotcha The types are generated from Chromium's HEAD revision, meaning they might occasionally reflect APIs not yet available in the current *stable* Chrome browser release. This can lead to TypeScript errors for valid API calls in older browser versions or vice-versa.
Install
-
npm install chrome-types -
yarn add chrome-types -
pnpm add chrome-types
Imports
- Global 'chrome' object
import { chrome } from 'chrome-types'Install 'chrome-types' as a dev dependency and configure `tsconfig.json` to include types (often implicit).
- Deprecated Platform Apps APIs
/// <reference types="chrome-types" /> (does not include deprecated APIs by default)
/// <reference types="chrome-types/lib/_all" />
Quickstart
{
"compilerOptions": {
"target": "es2020",
"module": "esnext",
"lib": ["es2020", "dom"],
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"types": ["chrome-types"], // Explicitly include chrome-types
"moduleResolution": "node"
},
"include": ["src/**/*.ts", "manifest.json"],
"exclude": ["node_modules"]
}
// src/background.ts
chrome.runtime.onInstalled.addListener(() => {
console.log('Chrome extension installed.');
chrome.storage.local.set({ greeting: 'Hello, Chrome Extensions!' }, () => {
console.log('Initial greeting saved.');
});
});
chrome.action.onClicked.addListener(async (tab) => {
console.log(`Action clicked on tab: ${tab?.id}`);
const data = await chrome.storage.local.get('greeting');
console.log('Retrieved greeting:', data.greeting);
if (tab?.id) {
// Example: Injecting a content script programmatically
chrome.scripting.executeScript({
target: { tabId: tab.id },
function: () => {
alert('Hello from your Chrome extension!');
},
});
}
});