Skip to content

Commit

Permalink
Added InjectableCompat (#11)
Browse files Browse the repository at this point in the history
Addresses #10

Users of TS4 and earlier can now use `InjectableCompat` to be able to
specify dependencies tuple.

---------

Co-authored-by: Konstantin Burov <[email protected]>
  • Loading branch information
mikalai-snap and kburov-sc authored Nov 4, 2024
1 parent 60a89bf commit bb78e35
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@snap/ts-inject",
"version": "0.2.0",
"version": "0.3.0",
"description": "100% typesafe dependency injection framework for TypeScript projects",
"license": "MIT",
"author": "Snap Inc.",
Expand Down
29 changes: 24 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,28 @@ 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
): ReturnType<typeof Injectable> {
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";

0 comments on commit bb78e35

Please sign in to comment.