webextension-polyfill-ts
webextension-polyfill-ts copied to clipboard
Improve type safety in the Storage API
any
is inherently unsafe and hurts the boundary with your application code. Using unknown will require consumers to do type checks before using values which is what they should be doing anyways
Another way to do this (somewhat) more safely would be to add generics that default to unknown
. Something like this:
interface StorageArea {
get<T = unknown>(key: P): Promise<Record<P, T>>;
}
This wouldn't be as typesafe as just using unknown everywhere, since users can directly manipulate values in storage outside of the application, but it would at least improve the type safety within the application itself.
Thanks. You're right and I think this is the case for other places as well. I've created a PR: #97, which converts as many of the anys to unknown as possible. Not entirely sure if this is correct for every situation. Do you mind reviewing the out files on that PR? I'll double-check as well, but my brain is fried for today.