Skip to content

Commit bb78e35

Browse files
Added InjectableCompat (#11)
Addresses #10 Users of TS4 and earlier can now use `InjectableCompat` to be able to specify dependencies tuple. --------- Co-authored-by: Konstantin Burov <kburov@snapchat.com>
1 parent 60a89bf commit bb78e35

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@snap/ts-inject",
3-
"version": "0.2.0",
3+
"version": "0.3.0",
44
"description": "100% typesafe dependency injection framework for TypeScript projects",
55
"license": "MIT",
66
"author": "Snap Inc.",

src/Injectable.ts

+24-5
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,8 @@ export function Injectable<Token extends TokenType, Service>(
2828
* The dependencies are specified as tokens, and the factory function
2929
* will receive these dependencies as arguments in the order they are listed.
3030
*
31-
* **Note:** Dependencies must be specified as constant literals to allow TypeScript to ensure type safety.
32-
*
33-
* **Note:** Starting with TypeScript version 5, the `as const` assertion in the example below is not needed
34-
* due to the introduction of [const type parameters feature](
35-
* https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-0.html#const-type-parameters).
31+
* **Important:** This function requires **TypeScript 5 or later** due to the use of `const` type parameters.
32+
* Users on TypeScript 4 and earlier must use {@link InjectableCompat} instead.
3633
*
3734
* @example
3835
* ```ts
@@ -100,6 +97,28 @@ export function Injectable(
10097
return factory;
10198
}
10299

100+
/**
101+
* A compatibility version of {@link Injectable} for TypeScript 4 and earlier users.
102+
* This function behaves identically to {@link Injectable} but requires the use of `as const` on the dependencies array.
103+
*
104+
* @deprecated Use {@link Injectable} instead. This function is provided for compatibility with TypeScript 4
105+
* and earlier versions and will be removed in future releases.
106+
*
107+
* @see {@link Injectable} for detailed usage instructions and examples.
108+
*/
109+
export function InjectableCompat<
110+
Token extends TokenType,
111+
Tokens extends readonly TokenType[],
112+
Params extends readonly any[],
113+
Service,
114+
>(
115+
token: Token,
116+
dependencies: Tokens,
117+
fn: (...args: Tokens["length"] extends Params["length"] ? Params : void[]) => Service
118+
): ReturnType<typeof Injectable> {
119+
return Injectable(token, dependencies, fn);
120+
}
121+
103122
/**
104123
* Creates an Injectable factory function for an InjectableClass.
105124
*

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export { CONTAINER, Container } from "./Container";
2-
export { Injectable, ConcatInjectable } from "./Injectable";
2+
export { Injectable, InjectableCompat, ConcatInjectable } from "./Injectable";
33
export { PartialContainer } from "./PartialContainer";
44
export { InjectableFunction, InjectableClass, ServicesFromInjectables } from "./types";

0 commit comments

Comments
 (0)