Skip to content

Commit

Permalink
feat(core): optimize types to decrease building time (#25)
Browse files Browse the repository at this point in the history
Co-authored-by: Dmitrii Kamenskikh <[email protected]>
  • Loading branch information
coolassassin and Dmitrii Kamenskikh authored Jan 7, 2025
1 parent b954c6c commit b21d193
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 67 deletions.
19 changes: 14 additions & 5 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -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'
88 changes: 40 additions & 48 deletions packages/core/src/injectable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,29 +97,27 @@ type MapInjectablesToValues<Targets> = {
// readonly [Key in keyof Inputs]: InjectableValue<Inputs[Key]>
// }
// >
export interface DependencyWithoutName<Result, Children> {
readonly name: never
readonly type: Result
readonly optional: false
readonly children: Children
}

export interface DependencyWithName<Name, Result, Children> {
readonly name: Name
readonly type: Result
readonly optional: true
readonly children: Children
}

export function injectable<Name extends PropertyKey, Result>(
name: Name,
project: () => Result
): InjectableWithName<
{
readonly name: Name
readonly type: Result
readonly optional: true
readonly children: []
},
Result
>
): InjectableWithName<DependencyWithName<Name, Result, []>, Result>
export function injectable<Result>(
project: () => Result
): InjectableWithoutName<
{
readonly name: never
readonly type: Result
readonly optional: false
readonly children: []
},
Result
>
): InjectableWithoutName<DependencyWithoutName<Result, []>, Result>
export function injectable<
Inputs extends Record<
PropertyKey,
Expand All @@ -132,14 +130,12 @@ export function injectable<
readonly [Key in keyof Inputs]: InjectableValue<Inputs[Key]>
}) => Result
): InjectableWithoutName<
{
readonly name: never
readonly type: Result
readonly optional: false
readonly children: {
DependencyWithoutName<
Result,
{
[Key in keyof Inputs]: InjectableDependencyTree<Inputs[Key]>
}[keyof Inputs][]
},
>,
Result
>
export function injectable<
Expand All @@ -156,14 +152,13 @@ export function injectable<
readonly [Key in keyof Inputs]: InjectableValue<Inputs[Key]>
}) => Result
): InjectableWithName<
{
readonly name: Name
readonly type: Result
readonly optional: true
readonly children: {
DependencyWithName<
Name,
Result,
{
[Key in keyof Inputs]: InjectableDependencyTree<Inputs[Key]>
}[keyof Inputs][]
},
>,
Result
>
// export function injectable<
Expand All @@ -188,36 +183,33 @@ export function injectable<
export function injectable<
Name extends PropertyKey,
Inputs extends readonly Injectable<UnknownDependencyTree, unknown>[],
Value
Result
>(
name: Name,
...args: [...Inputs, (...values: MapInjectablesToValues<Inputs>) => Value]
...args: [...Inputs, (...values: MapInjectablesToValues<Inputs>) => Result]
): InjectableWithName<
{
readonly name: Name
readonly type: Value
readonly optional: true
readonly children: {
DependencyWithName<
Name,
Result,
{
readonly [Index in keyof Inputs]: InjectableDependencyTree<Inputs[Index]>
}
},
Value
>,
Result
>
export function injectable<
Inputs extends readonly Injectable<UnknownDependencyTree, unknown>[],
Value
Result
>(
...args: [...Inputs, (...values: MapInjectablesToValues<Inputs>) => Value]
...args: [...Inputs, (...values: MapInjectablesToValues<Inputs>) => Result]
): InjectableWithoutName<
{
readonly name: never
readonly type: Value
readonly optional: false
readonly children: {
DependencyWithoutName<
Result,
{
readonly [Index in keyof Inputs]: InjectableDependencyTree<Inputs[Index]>
}
},
Value
>,
Result
>
/* @__NO_SIDE_EFFECTS__ */
export function injectable(
Expand Down
25 changes: 11 additions & 14 deletions packages/core/src/token.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { InjectableWithName } from './injectable'
import { DependencyWithName, InjectableWithName } from './injectable'

export const TOKEN_ACCESSOR_KEY = '@injectable-ts/core//TOKEN_ACCESSOR'

Expand All @@ -9,23 +9,20 @@ export interface TokenAccessor {
): Dependencies[Name]
}

export interface TokenInjectable<Name, Type> {
readonly name: Name
readonly type: Type
readonly optional: false
readonly children: readonly [
DependencyWithName<typeof TOKEN_ACCESSOR_KEY, TokenAccessor, []>
]
}

/* @__NO_SIDE_EFFECTS__ */ export function token<Name extends PropertyKey>(
name: Name
) {
return <Type = never>(): 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<Name, Type>,
Type
> => {
const f = (
Expand Down

0 comments on commit b21d193

Please sign in to comment.