udic 2.0.3 → 2.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.d.ts +12 -11
  2. package/index.js +1 -1
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -2,28 +2,29 @@ type ObjectUnionToIntersect<T> = { [K in T as K & string] : K }[any];
2
2
  export type ContextArgs = {
3
3
  [key: string | symbol]: any
4
4
  } | ((...args: any[]) => any);
5
- export type Context<Deps extends ContextArgs> = ObjectUnionToIntersect<Deps extends (() => infer Out extends {}) ? Awaited<Out> : Deps extends ((c: infer In extends {}) => infer Out extends {}) ? In & Awaited<Out> : Deps extends (c: infer C, ...args: any[]) => any ? C : Deps>;
5
+ export type Context<Deps extends ContextArgs> = ObjectUnionToIntersect<Deps extends ImplTag<infer C> ? C : Deps extends (c: infer C, ...args: any[]) => any ? C : Deps>;
6
+ export interface ImplTag<Context> {
7
+ "~infer": Context;
8
+ }
6
9
  export interface Impl {
7
- <const F extends () => {}>(fn: F): F;
8
- <const F extends (c: any) => {}>(fn: F): F;
9
- <const Out extends ContextArgs>(fn: (c: {}) => Context<Out>): (c: {}) => Context<Out>;
10
+ <const F>(fn: F): F & ImplTag<F extends (() => infer Out extends {}) ? Out : F extends ((c: infer In extends {}) => infer Out extends {}) ? In & Out : never>;
11
+ <const Out extends ContextArgs>(fn: () => Context<Out>): (() => Context<Out>) & ImplTag<Context<Out>>;
10
12
  <
11
13
  const In extends ContextArgs,
12
14
  const Out extends ContextArgs
13
- >(fn: (c: Context<In>) => Context<Out>): (c: Context<In>) => Context<Out>;
15
+ >(fn: (c: Context<In>) => Context<Out>): ((c: Context<In>) => Context<Out>) & ImplTag<Context<In> & Context<Out>>;
14
16
  }
15
17
  /**
16
18
  * Create a sync implementation of a dependency.
17
19
  */
18
20
  export declare const impl: Impl;
19
21
  export interface ImplAsync {
20
- <const F extends () => {} | Promise<{}>>(fn: F): F;
21
- <const F extends (c: any) => {} | Promise<{}>>(fn: F): F;
22
- <const Out extends ContextArgs>(fn: (c: {}) => Context<Out> | Promise<Context<Out>>): (c: {}) => Context<Out> | Promise<Context<Out>>;
22
+ <const F>(fn: F): F & ImplTag<F extends (() => infer Out extends {} | Promise<{}>) ? Awaited<Out> : F extends ((c: infer In extends {}) => infer Out extends {} | Promise<{}>) ? In & Awaited<Out> : never>;
23
+ <const Out extends ContextArgs>(fn: () => Context<Out> | Promise<Context<Out>>): (() => Context<Out>) & ImplTag<Context<Out>>;
23
24
  <
24
25
  const In extends ContextArgs,
25
26
  const Out extends ContextArgs
26
- >(fn: (c: Context<In>) => Context<Out> | Promise<Context<Out>>): (c: Context<In>) => Context<Out> | Promise<Context<Out>>;
27
+ >(fn: (c: Context<In>) => Context<Out> | Promise<Context<Out>>): ((c: Context<In>) => Context<Out> | Promise<Context<Out>>) & ImplTag<Context<In> & Context<Out>>;
27
28
  }
28
29
  /**
29
30
  * Create an async implementation of a dependency.
@@ -37,12 +38,12 @@ type _ImplFns = [_ImplFn, ..._ImplFn[]];
37
38
  export declare const link: <
38
39
  const In extends {},
39
40
  Impls extends _ImplFns
40
- >(c: In, ...impls: Impls) => In & ReturnType<Impls[number]>;
41
+ >(c: In, ...impls: Impls) => Context<Impls[number]>;
41
42
  /**
42
43
  * Link async implementations concurrently.
43
44
  */
44
45
  export declare const linkAsync: <
45
46
  const In extends {},
46
47
  Impls extends _ImplFns
47
- >(c: In, ...impls: Impls) => Promise<In & Awaited<ReturnType<Impls[number]>>>;
48
+ >(c: In, ...impls: Impls) => Promise<Context<Impls[number]>>;
48
49
  export {};
package/index.js CHANGED
@@ -1 +1 @@
1
- export let impl=e=>e;export let implAsync=e=>e;export let link=(e,t,...n)=>{e=Object.assign(t(e),e);for(let t=0;t<n.length;t++)Object.assign(e,n[t](e));return e};export let linkAsync=async(e,...t)=>{if(t.length===1)return Object.assign(await t[0](e),e);let n=Array(t.length);for(let r=0;r<t.length;r++)n[r]=t[r](e);return Object.assign(...await Promise.all(n),e)};
1
+ export let impl=e=>e;export let implAsync=e=>e;export let link=(e,...t)=>{for(let n=0;n<t.length;n++)Object.assign(e,t[n](e));return e};export let linkAsync=async(e,...t)=>{if(t.length===1)return Object.assign(e,await t[0](e));let n=Array(t.length);for(let r=0;r<t.length;r++)n[r]=t[r](e);return Object.assign(e,...await Promise.all(n))};
package/package.json CHANGED
@@ -1 +1 @@
1
- {"name":"udic","version":"2.0.3","description":"A simple dependency injection library","keywords":["di"],"license":"MIT","homepage":"https://re-utils.pages.dev/di","repository":{"type":"git","url":"git+https://github.com/re-utils/di.git"},"type":"module","main":"./index.js","types":"./index.d.ts","exports":{".":"./index.js"}}
1
+ {"name":"udic","version":"2.0.5","description":"A simple dependency injection library","keywords":["di"],"license":"MIT","homepage":"https://re-utils.pages.dev/di","repository":{"type":"git","url":"git+https://github.com/re-utils/di.git"},"type":"module","main":"./index.js","types":"./index.d.ts","exports":{".":"./index.js"}}