Skip to content

Commit

Permalink
feat: createDefineConfig and DefineConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Mar 14, 2023
1 parent 538915e commit 40a921e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
12 changes: 12 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,15 @@ export interface LoadConfigOptions<
extendKey?: string | string[];
};
}

export type DefineConfig<
T extends UserInputConfig = UserInputConfig,
MT extends ConfigLayerMeta = ConfigLayerMeta
> = (input: InputConfig<T, MT>) => InputConfig<T, MT>;

export function createDefineConfig<
T extends UserInputConfig = UserInputConfig,
MT extends ConfigLayerMeta = ConfigLayerMeta
>(): DefineConfig<T, MT> {
return (input: InputConfig<T, MT>) => input;
}
8 changes: 4 additions & 4 deletions test/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expectTypeOf } from "expect-type";
import { loadConfig, InputConfig } from "../src";
import { loadConfig, InputConfig, createDefineConfig } from "../src";

interface MyConfig {
foo: string;
Expand All @@ -9,17 +9,17 @@ interface MyMeta {
metaFoo: string;
}

type UserConfig = InputConfig<MyConfig, MyMeta>;
const defineMyConfig = createDefineConfig<MyConfig, MyMeta>();

const userConfig: UserConfig = {
const userConfig = defineMyConfig({
foo: "bar",
$meta: {
metaFoo: "bar",
},
$development: {
foo: "bar",
},
};
});

expectTypeOf(userConfig.$production!.foo).toEqualTypeOf<string>();
expectTypeOf(userConfig.$meta!.metaFoo).toEqualTypeOf<string>();
Expand Down

0 comments on commit 40a921e

Please sign in to comment.