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

Optimize types to decrease building time #25

Merged
merged 1 commit into from
Jan 7, 2025
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
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
Loading