A consolidated list for type definition referencing
There's a few posts here on SO for chrome object typing issues, actually a lot of good information on the subject, but the approach will vary by your environment and need, a good post to look into: How to use chrome-app.d.ts type in angular-cli 1.0.1 app?
For the love of god, just gimme them TYPES!
I've had success with all 3 of these approaches in various projects; however, more people than I care to admit ask me about types, specifically the chrome extension API object... So, stumbled upon this post and am just sharing a few, they're all rather common but easy to forget so here's a refresher!
If there are others ways I do not encapsulate here, please reach out so I can include them
1. Automatic type acquisition
This automatically acquires the necessary type definitions and includes the references in your project without a /// <reference types="..." /> call.
This approach is more beneficial in vanilla Javascript projects (with or without a tsconfig) where types may not be easily reached from the terminal using npm since it may not be used in the project. Either way, it's a neat approach.
// jsconfig.json
{
"typeAcquisition": {
"include": ["chrome"]
}
}
2. Type definition compilation
Here, tsc explicitly includes the type definitions by referencing them from node_modules once the below snippet is implemented, making the types available globally in your project.
This approach requires that you install npm i -d @types/chrome in your project.
// tsconfig.json
{
"compilerOptions": {
"types": ["chrome"]
}
}
2b. A quick note for the Angular-specific approach...
As notated here: Cannot find namespace/name chrome—basically, Angular is special, it is the same as the above only specific to Angular, for reasons.
{
"angularCompilerOptions": {
"types": ["chrome"]
}
}
3. Manually download, reference, local type definition files
This approach seems to work the best for me and my local IDE configurations, but that will vary from person to person, it does not require a tsconfig or jsconfig file to implement either.
sw.js (or background.js) is probably the safest place to put this for chrome extension development, but I haven't had issues so long as it is included somewhere at least once (even if it is referenced in deferred scripts it seems to work fine)
This is a triple-slash directive XML reference approach that seems random yet it simply works. The other approaches I have to run commands or reload my IDE, they dismount randomly, etc. With this, I had type definitions before I even saved the file.
//! sw.js
/// <reference types="~/dev/@types/chrome.d.ts" />
Referencing the raw github file directly here, sadly, does not work. (e.g. https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/refs/heads/master/types/chrome/index.d.ts). I also cannot locate any CDN sources for these definitions either… SO, to proceed, you need to download the above d.ts file, save it locally (probably in your project), and reference the file with a relative path with the following comment tag.
And that's that, if you see anything wrong or have a better way to articulate something, as always, feel free to edit and update or comment. I was tired of searching for bookmarks on all these approaches when devs asked me about it, so consolidated them here.
window['chrome']