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

Added InjectableCompat #11

Merged
merged 5 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
31 changes: 26 additions & 5 deletions src/Injectable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,8 @@ export function Injectable<Token extends TokenType, Service>(
* The dependencies are specified as tokens, and the factory function
* will receive these dependencies as arguments in the order they are listed.
*
* **Note:** Dependencies must be specified as constant literals to allow TypeScript to ensure type safety.
*
* **Note:** Starting with TypeScript version 5, the `as const` assertion in the example below is not needed
* due to the introduction of [const type parameters feature](
* https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-0.html#const-type-parameters).
* **Important:** This function requires **TypeScript 5 or later** due to the use of `const` type parameters.
* Users on TypeScript 4 and earlier must use {@link InjectableCompat} instead.
*
* @example
* ```ts
Expand Down Expand Up @@ -100,6 +97,30 @@ export function Injectable(
return factory;
}

/**
* A compatibility version of {@link Injectable} for TypeScript 4 and earlier users.
* This function behaves identically to {@link Injectable} but requires the use of `as const` on the dependencies array.
*
* @deprecated Use {@link Injectable} instead. This function is provided for compatibility with TypeScript 4
* and earlier versions and will be removed in future releases.
*
* @see {@link Injectable} for detailed usage instructions and examples.
*/
export function InjectableCompat<
Token extends TokenType,
Tokens extends readonly TokenType[],
Params extends readonly any[],
Service,
>(
token: Token,
dependencies: Tokens,
fn: (...args: Tokens["length"] extends Params["length"] ? Params : void[]) => Service
): Tokens["length"] extends Params["length"]
? InjectableFunction<ServicesFromTokenizedParams<Tokens, Params>, Tokens, Token, Service>
mikalai-snap marked this conversation as resolved.
Show resolved Hide resolved
: never {
return Injectable(token, dependencies, fn);
}

/**
* Creates an Injectable factory function for an InjectableClass.
*
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { CONTAINER, Container } from "./Container";
export { Injectable, ConcatInjectable } from "./Injectable";
export { Injectable, InjectableCompat, ConcatInjectable } from "./Injectable";
export { PartialContainer } from "./PartialContainer";
export { InjectableFunction, InjectableClass, ServicesFromInjectables } from "./types";