Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: validate dependency types in registry #6

Merged
merged 2 commits into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ export type ProviderFn<
? (externalDeps?: ProviderFnArgs<Registry>) => ModuleAPI<Registry, PublicAPI>
: (externalDeps: ProviderFnArgs<Registry>) => ModuleAPI<Registry, PublicAPI>;

/**
* Checks if each injectable match the required dependencies of the entire registry
*/
type ValidateRegistry<Registry extends ObjectLike, Deps = FlatDependencyTree<Registry>> = {
[key in keyof Registry]: key extends keyof Deps ? ((deps?: any) => Deps[key]) | Deps[key] : Registry[key];
};

declare function valueFn<T>(value: T): () => T;

declare const provideSymbol: unique symbol;
Expand All @@ -91,7 +98,7 @@ declare function createProvider<
Registry extends ObjectLike,
PublicAPI extends Array<keyof Registry> = []
>(args: {
injectables: Registry;
injectables: ValidateRegistry<Registry>;
api?: PublicAPI;
}): ProviderFn<Registry, PublicAPI>;

Expand Down
42 changes: 24 additions & 18 deletions src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,31 +203,28 @@ createProvider: {
},
})({});

let someProvider = createProvider({
createProvider({
injectables: {
a: ({ b }: { b: string }) => b,
b: () => 42,
foo: ({ a }: { a: number }) => a,
bar: ({ b }: { b: string }) => b,
// @ts-expect-error a is not a number
a: "42",
// @ts-expect-error b is not a string
b: () => 42 as number,
},
api: ['a'],
api: ['foo', 'bar'],
});

// @ts-expect-error
// b is not a number
someProvider();

let f = createProvider({
createProvider({
injectables: {
a: ({ b }: { b: number }) => b,
c: ({ b }: { b: string }) => b,
b: 42,
a: ({ c }: { c: number }) => c,
b: ({ c }: { c: string }) => c,
// @ts-expect-error c does not satisfy a & b
c: 42,
},
api: ['a'],
api: ['a', 'c'],
});

// @ts-expect-error
// b can not be fulfilled
f();

// when all dependencies are provide, external Deps is optional
const provideFulfilled = createProvider({
injectables: {
Expand All @@ -253,11 +250,20 @@ createProvider: {
provideMissing();
// @ts-expect-error
provideMissing({});

const provideWrongType = createProvider({
injectables: {
foo: ({ val }: { val: number }) => val,
},
api: ['foo'],
});
// @ts-expect-error wrong dependency type
provideWrongType({ val: "42" })
}

fromClass: {
class Foo {
constructor({ b }: { b: string }) {}
constructor({ b }: { b: string }) { }
}
let factory = fromClass(Foo);
factory({ b: 'woot' });
Expand Down
Loading