extension: Type `utils.getOptions()` better

This disallows passing non-existent options:

```ts
utils.getOptions(["doesNotExist"]); // error
```

And also disallows using options that were not requested:

```ts
const options = utils.getOptions(["ruffleEnable"]);
options.ignoreOptout; // error
```
This commit is contained in:
relrelb 2021-09-24 09:33:02 +03:00 committed by relrelb
parent 34facdc5c2
commit 8c19e06e5e
1 changed files with 4 additions and 2 deletions

View File

@ -1,6 +1,6 @@
import { Options } from "./common";
const DEFAULT_OPTIONS = {
const DEFAULT_OPTIONS: Options = {
ruffleEnable: true,
ignoreOptout: false,
};
@ -194,7 +194,9 @@ if (typeof chrome !== "undefined") {
throw new Error("Extension API not found.");
}
export async function getOptions(keys: string[]): Promise<Options> {
export async function getOptions<T extends keyof Options>(
keys: T[]
): Promise<Pick<Options, T>> {
const options = await storage.sync.get(keys);
// Copy over default options if they don't exist yet.