Skip to content

Commit

Permalink
fix(store,world): throw on unexpected config keys (#2797)
Browse files Browse the repository at this point in the history
  • Loading branch information
holic committed May 8, 2024
1 parent 4caca05 commit 32c1cda
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .changeset/silver-toys-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@latticexyz/store": patch
"@latticexyz/world": patch
---

`defineStore` and `defineWorld` will now throw a type error if an unexpected config option is used.
9 changes: 9 additions & 0 deletions packages/store/ts/config/v2/store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,4 +516,13 @@ describe("defineStore", () => {

defineStore(config);
});

it("should throw if config has unexpected key", () => {
attest(() =>
defineStore({
// @ts-expect-error Invalid config option
invalidOption: "nope",
}),
).type.errors("`invalidOption` is not a valid Store config option.");
});
});
4 changes: 2 additions & 2 deletions packages/store/ts/config/v2/store.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { flatMorph, narrow } from "@arktype/util";
import { ErrorMessage, flatMorph, narrow } from "@arktype/util";
import { get, hasOwnKey, mergeIfUndefined } from "./generics";
import { UserTypes } from "./output";
import { CONFIG_DEFAULTS } from "./defaults";
Expand All @@ -23,7 +23,7 @@ export type validateStore<store> = {
? narrow<store[key]>
: key extends keyof StoreInput
? StoreInput[key]
: never;
: ErrorMessage<`\`${key & string}\` is not a valid Store config option.`>;
};

export function validateStore(store: unknown): asserts store is StoreInput {
Expand Down
9 changes: 9 additions & 0 deletions packages/world/ts/config/v2/world.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -752,4 +752,13 @@ describe("defineWorld", () => {

defineWorld(config);
});

it("should throw if config has unexpected key", () => {
attest(() =>
defineWorld({
// @ts-expect-error Invalid config option
invalidOption: "nope",
}),
).type.errors("`invalidOption` is not a valid World config option.");
});
});
2 changes: 1 addition & 1 deletion packages/world/ts/config/v2/world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export type validateWorld<world> = {
ErrorMessage<`Namespaces config will be enabled soon.`>
: key extends keyof WorldInput
? conform<world[key], WorldInput[key]>
: world[key];
: ErrorMessage<`\`${key & string}\` is not a valid World config option.`>;
};

export function validateWorld(world: unknown): asserts world is WorldInput {
Expand Down

0 comments on commit 32c1cda

Please sign in to comment.