From e748791bb91af8b176ba4a237f23d3e1e0a277de Mon Sep 17 00:00:00 2001 From: Mikalai Silivonik Date: Mon, 21 Oct 2024 12:52:09 -0400 Subject: [PATCH 1/4] added InjectableCompat --- src/Injectable.ts | 24 ++++++++++++++++++++++++ src/index.ts | 2 +- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/Injectable.ts b/src/Injectable.ts index 01e5311..ece832b 100644 --- a/src/Injectable.ts +++ b/src/Injectable.ts @@ -100,6 +100,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, Tokens, Token, Service> + : never { + return Injectable(token, dependencies, fn); +} + /** * Creates an Injectable factory function for an InjectableClass. * diff --git a/src/index.ts b/src/index.ts index 71c0c0f..d17449d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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"; From 9ac0ed3839bb370a342621b34e4fd5622746cc61 Mon Sep 17 00:00:00 2001 From: Mikalai Silivonik Date: Mon, 28 Oct 2024 13:43:40 -0400 Subject: [PATCH 2/4] added comment --- src/Injectable.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/Injectable.ts b/src/Injectable.ts index ece832b..f3aa7d4 100644 --- a/src/Injectable.ts +++ b/src/Injectable.ts @@ -28,11 +28,8 @@ export function Injectable( * 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 From b5e70562740d4e3f200d72a5d93995083b789429 Mon Sep 17 00:00:00 2001 From: Mikalai Silivonik Date: Mon, 28 Oct 2024 21:21:03 -0400 Subject: [PATCH 3/4] Update src/Injectable.ts Co-authored-by: Konstantin Burov --- src/Injectable.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Injectable.ts b/src/Injectable.ts index f3aa7d4..052b750 100644 --- a/src/Injectable.ts +++ b/src/Injectable.ts @@ -115,9 +115,7 @@ export function InjectableCompat< token: Token, dependencies: Tokens, fn: (...args: Tokens["length"] extends Params["length"] ? Params : void[]) => Service -): Tokens["length"] extends Params["length"] - ? InjectableFunction, Tokens, Token, Service> - : never { +): ReturnType { return Injectable(token, dependencies, fn); } From ba1c5a797d27d6937273f6ccd547125f6c91f01b Mon Sep 17 00:00:00 2001 From: Mikalai Silivonik Date: Fri, 1 Nov 2024 15:14:46 -0400 Subject: [PATCH 4/4] 0.3.0 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 96b4c43..55a2e51 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@snap/ts-inject", - "version": "0.1.0", + "version": "0.3.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@snap/ts-inject", - "version": "0.1.0", + "version": "0.3.0", "license": "MIT", "devDependencies": { "@types/jest": "^29.5.12", diff --git a/package.json b/package.json index 72711ed..4e48b59 100644 --- a/package.json +++ b/package.json @@ -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.",