diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index bbf58f9..578571d 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -1,9 +1,13 @@ export { - Injectable, - InjectableValue, - InjectableDependencies, + type Injectable, + type InjectableValue, + type InjectableDependencies, + type InjectableWithName, + type InjectableWithoutName, + type InjectableValueWithoutName, + type InjectableValueWithName, injectable, - UnknownDependencyTree, + type UnknownDependencyTree, } from './injectable' export { provide } from './provide' -export { TokenAccessor, TOKEN_ACCESSOR_KEY, token } from './token' +export { type TokenAccessor, type TokenInjectable, TOKEN_ACCESSOR_KEY, token } from './token' diff --git a/packages/core/src/injectable.ts b/packages/core/src/injectable.ts index 1bdc266..c6d61d5 100644 --- a/packages/core/src/injectable.ts +++ b/packages/core/src/injectable.ts @@ -97,29 +97,27 @@ type MapInjectablesToValues = { // readonly [Key in keyof Inputs]: InjectableValue // } // > +export interface InjectableValueWithoutName { + readonly name: never + readonly type: Result + readonly optional: false + readonly children: Children +} + +export interface InjectableValueWithName { + readonly name: Name + readonly type: Result + readonly optional: true + readonly children: Children +} + export function injectable( name: Name, project: () => Result -): InjectableWithName< - { - readonly name: Name - readonly type: Result - readonly optional: true - readonly children: [] - }, - Result -> +): InjectableWithName, Result> export function injectable( project: () => Result -): InjectableWithoutName< - { - readonly name: never - readonly type: Result - readonly optional: false - readonly children: [] - }, - Result -> +): InjectableWithoutName, Result> export function injectable< Inputs extends Record< PropertyKey, @@ -132,14 +130,12 @@ export function injectable< readonly [Key in keyof Inputs]: InjectableValue }) => Result ): InjectableWithoutName< - { - readonly name: never - readonly type: Result - readonly optional: false - readonly children: { + InjectableValueWithoutName< + Result, + { [Key in keyof Inputs]: InjectableDependencyTree }[keyof Inputs][] - }, + >, Result > export function injectable< @@ -156,14 +152,13 @@ export function injectable< readonly [Key in keyof Inputs]: InjectableValue }) => Result ): InjectableWithName< - { - readonly name: Name - readonly type: Result - readonly optional: true - readonly children: { + InjectableValueWithName< + Name, + Result, + { [Key in keyof Inputs]: InjectableDependencyTree }[keyof Inputs][] - }, + >, Result > // export function injectable< @@ -188,36 +183,33 @@ export function injectable< export function injectable< Name extends PropertyKey, Inputs extends readonly Injectable[], - Value + Result >( name: Name, - ...args: [...Inputs, (...values: MapInjectablesToValues) => Value] + ...args: [...Inputs, (...values: MapInjectablesToValues) => Result] ): InjectableWithName< - { - readonly name: Name - readonly type: Value - readonly optional: true - readonly children: { + InjectableValueWithName< + Name, + Result, + { readonly [Index in keyof Inputs]: InjectableDependencyTree } - }, - Value + >, + Result > export function injectable< Inputs extends readonly Injectable[], - Value + Result >( - ...args: [...Inputs, (...values: MapInjectablesToValues) => Value] + ...args: [...Inputs, (...values: MapInjectablesToValues) => Result] ): InjectableWithoutName< - { - readonly name: never - readonly type: Value - readonly optional: false - readonly children: { + InjectableValueWithoutName< + Result, + { readonly [Index in keyof Inputs]: InjectableDependencyTree } - }, - Value + >, + Result > /* @__NO_SIDE_EFFECTS__ */ export function injectable( diff --git a/packages/core/src/token.ts b/packages/core/src/token.ts index c293f5c..abfd13e 100644 --- a/packages/core/src/token.ts +++ b/packages/core/src/token.ts @@ -1,4 +1,4 @@ -import { InjectableWithName } from './injectable' +import { InjectableValueWithName, InjectableWithName } from './injectable' export const TOKEN_ACCESSOR_KEY = '@injectable-ts/core//TOKEN_ACCESSOR' @@ -9,23 +9,20 @@ export interface TokenAccessor { ): Dependencies[Name] } +export interface TokenInjectable { + readonly name: Name + readonly type: Type + readonly optional: false + readonly children: readonly [ + InjectableValueWithName + ] +} + /* @__NO_SIDE_EFFECTS__ */ export function token( name: Name ) { return (): InjectableWithName< - { - readonly name: Name - readonly type: Type - readonly optional: false - readonly children: readonly [ - { - readonly name: typeof TOKEN_ACCESSOR_KEY - readonly type: TokenAccessor - readonly optional: true - readonly children: readonly [] - } - ] - }, + TokenInjectable, Type > => { const f = (