Skip to content

Commit

Permalink
fix mapped type
Browse files Browse the repository at this point in the history
  • Loading branch information
holic committed Jul 9, 2024
1 parent 14dd5e8 commit 8ee055b
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions packages/store/ts/config/v2/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { ErrorMessage, evaluate, narrow } from "@arktype/util";
import { get, hasOwnKey, mergeIfUndefined } from "./generics";
import { UserTypes } from "./output";
import { CONFIG_DEFAULTS } from "./defaults";
import { NamespaceInput, StoreInput } from "./input";
import { StoreInput } from "./input";
import { validateTables } from "./tables";
import { scopeWithUserTypes, validateUserTypes } from "./userTypes";
import { mapEnums, resolveEnums, scopeWithEnums } from "./enums";
import { resolveCodegen } from "./codegen";
Expand All @@ -14,16 +15,19 @@ export function extendedScope<input>(input: input): extendedScope<input> {
return scopeWithEnums(get(input, "enums"), scopeWithUserTypes(get(input, "userTypes")));
}

export type validateStore<input> = (input extends NamespaceInput
? validateNamespace<Pick<input, keyof NamespaceInput>, extendedScope<input>>
: {}) & {
[key in keyof input]: key extends "userTypes"
? UserTypes
: key extends "enums"
? narrow<input[key]>
: key extends keyof StoreInput
? StoreInput[key]
: ErrorMessage<`\`${key & string}\` is not a valid Store config option.`>;
// Ideally we'd be able to use an intersection of `validateNamespace<...> & { ... }` to avoid
// duplicating logic, but TS doesn't work well when mapping over types like that.
// TODO: fill in more reasons why
export type validateStore<input> = {
[key in keyof input]: key extends "tables"
? validateTables<input[key], extendedScope<input>>
: key extends "userTypes"
? UserTypes
: key extends "enums"
? narrow<input[key]>
: key extends keyof StoreInput
? StoreInput[key]
: ErrorMessage<`\`${key & string}\` is not a valid Store config option.`>;
};

export function validateStore(input: unknown): asserts input is StoreInput {
Expand Down

0 comments on commit 8ee055b

Please sign in to comment.