webextension-polyfill-ts icon indicating copy to clipboard operation
webextension-polyfill-ts copied to clipboard

Improve type safety in the Storage API

Open dbasilio opened this issue 1 year ago • 1 comments

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.

dbasilio avatar Jul 10 '23 18:07 dbasilio

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.

Lusito avatar Sep 13 '23 19:09 Lusito