From b21d1932faed1a7a2ccc122f47936453c33ad388 Mon Sep 17 00:00:00 2001 From: Kamenskih Dmitriy Date: Tue, 7 Jan 2025 12:41:18 +0000 Subject: [PATCH] feat(core): optimize types to decrease building time (#25) Co-authored-by: Dmitrii Kamenskikh --- packages/core/src/index.ts | 19 +++++-- packages/core/src/injectable.ts | 88 +++++++++++++++------------------ packages/core/src/token.ts | 25 +++++----- 3 files changed, 65 insertions(+), 67 deletions(-) diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index bbf58f9..5879644 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -1,9 +1,18 @@ export { - Injectable, - InjectableValue, - InjectableDependencies, + type Injectable, + type InjectableValue, + type InjectableDependencies, + type InjectableWithName, + type InjectableWithoutName, + type DependencyWithoutName, + type DependencyWithName, 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..090d94b 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 DependencyWithoutName { + readonly name: never + readonly type: Result + readonly optional: false + readonly children: Children +} + +export interface DependencyWithName { + 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: { + DependencyWithoutName< + 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: { + DependencyWithName< + 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: { + DependencyWithName< + 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: { + DependencyWithoutName< + 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..8867cab 100644 --- a/packages/core/src/token.ts +++ b/packages/core/src/token.ts @@ -1,4 +1,4 @@ -import { InjectableWithName } from './injectable' +import { DependencyWithName, 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 [ + DependencyWithName + ] +} + /* @__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 = (