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:
parent
34facdc5c2
commit
8c19e06e5e
|
@ -1,6 +1,6 @@
|
||||||
import { Options } from "./common";
|
import { Options } from "./common";
|
||||||
|
|
||||||
const DEFAULT_OPTIONS = {
|
const DEFAULT_OPTIONS: Options = {
|
||||||
ruffleEnable: true,
|
ruffleEnable: true,
|
||||||
ignoreOptout: false,
|
ignoreOptout: false,
|
||||||
};
|
};
|
||||||
|
@ -194,7 +194,9 @@ if (typeof chrome !== "undefined") {
|
||||||
throw new Error("Extension API not found.");
|
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);
|
const options = await storage.sync.get(keys);
|
||||||
|
|
||||||
// Copy over default options if they don't exist yet.
|
// Copy over default options if they don't exist yet.
|
||||||
|
|
Loading…
Reference in New Issue