@anvil-di/anvil 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for describing the origin of the Work and
141
+ reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Support. While redistributing the Work or
166
+ Derivative Works thereof, You may choose to offer, and charge a
167
+ fee for, acceptance of support, warranty, indemnity, or other
168
+ liability obligations and/or rights consistent with this License.
169
+ However, in accepting such obligations, You may act only on Your
170
+ own behalf and on Your sole responsibility, not on behalf of any
171
+ other Contributor, and only if You agree to indemnify, defend,
172
+ and hold each Contributor harmless for any liability incurred by,
173
+ or claims asserted against, such Contributor by reason of your
174
+ accepting any such warranty or support.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2026 typescript-di contributors
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
200
+ implied. See the License for the specific language governing
201
+ permissions and limitations under the License.
@@ -0,0 +1,181 @@
1
+ /**
2
+ * No-op decorator stubs recognized by the `anvil` Rust codegen toolchain.
3
+ *
4
+ * These exist purely so user code typechecks (`import { Inject } from "@anvil-di/anvil"`).
5
+ * The actual semantics are implemented at compile time by reading the decorator
6
+ * AST and emitting wiring code into co-located `*.anvil.ts` files.
7
+ *
8
+ * All decorators are TC39 Stage-3 ([decision recorded in](../../../docs/adr/0002-stage3-decorators-only.md))
9
+ * and do not require `experimentalDecorators` in `tsconfig.json`.
10
+ */
11
+ /**
12
+ * Marks a class as a `@Module` whose static methods (annotated with `@Provides`)
13
+ * contribute bindings to a component's graph.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * import { Module, Provides } from "@anvil-di/anvil";
18
+ * import { Pump } from "./pump";
19
+ *
20
+ * @Module
21
+ * export class CoffeeModule {
22
+ * @Provides static providePump(p: Pump): Pump { return p; }
23
+ * }
24
+ * ```
25
+ */
26
+ export declare function Module<T extends abstract new (...args: never[]) => unknown>(target: T, _ctx: ClassDecoratorContext<T>): T;
27
+ /**
28
+ * Marks a static method on a `@Module` class as a binding factory.
29
+ *
30
+ * The method's return type is the bound key; its parameters are the dependencies.
31
+ * Must be `static` and have an explicit return type — both rules are enforced
32
+ * at codegen time.
33
+ */
34
+ export declare function Provides<This, Args extends readonly unknown[], Return>(target: (this: This, ...args: Args) => Return, _ctx: ClassMethodDecoratorContext<This, (this: This, ...args: Args) => Return>): (this: This, ...args: Args) => Return;
35
+ /**
36
+ * Marks a class for injection by the codegen toolchain. The class's
37
+ * constructor parameter types become the binding's dependencies.
38
+ *
39
+ * `@Inject` is a class-level decorator — TC39 Stage-3 decorators do not
40
+ * apply to constructors, so the legacy `@Inject constructor(...)` placement
41
+ * is rejected by the toolchain.
42
+ *
43
+ * @example
44
+ * ```ts
45
+ * import { Inject } from "@anvil-di/anvil";
46
+ * import { Heater } from "./heater";
47
+ *
48
+ * @Inject
49
+ * export class Pump {
50
+ * constructor(private heater: Heater) {}
51
+ * }
52
+ * ```
53
+ */
54
+ export declare function Inject<T extends abstract new (...args: never[]) => unknown>(target: T, _ctx: ClassDecoratorContext<T>): T;
55
+ /**
56
+ * Marks a static method on a `@Module` class as an alias-binding from
57
+ * the method's return type to its single parameter type. The codegen
58
+ * emits a factory that delegates straight to the parameter type's
59
+ * factory — useful for binding an interface (or abstract base class) to
60
+ * one concrete impl.
61
+ *
62
+ * The method must be `static`, take exactly one parameter, and declare
63
+ * an explicit return type. It must not also be `@Provides`. The body
64
+ * is required so the file type-checks, but the codegen ignores it and
65
+ * emits a delegate to the parameter's factory.
66
+ *
67
+ * (TC39 Stage-3 decorators cannot decorate `abstract` methods —
68
+ * TS error 1249 — which is why `@Binds` uses a static method with a
69
+ * trivial `return impl;` body rather than the Dagger-style abstract form.)
70
+ *
71
+ * @example
72
+ * ```ts
73
+ * import { Module, Binds } from "@anvil-di/anvil";
74
+ * import { Heater } from "./heater";
75
+ * import { ElectricHeater } from "./electric-heater";
76
+ *
77
+ * @Module
78
+ * export class CoffeeModule {
79
+ * @Binds static heater(impl: ElectricHeater): Heater { return impl; }
80
+ * }
81
+ * ```
82
+ */
83
+ export declare function Binds<This, Args extends readonly unknown[], Return>(target: (this: This, ...args: Args) => Return, _ctx: ClassMethodDecoratorContext<This, (this: This, ...args: Args) => Return>): (this: This, ...args: Args) => Return;
84
+ /**
85
+ * Configuration for a `@Component`.
86
+ */
87
+ export interface ComponentConfig {
88
+ /** Module classes whose `@Provides` methods are visible to this component. */
89
+ readonly modules?: ReadonlyArray<abstract new (...args: never[]) => unknown>;
90
+ }
91
+ /**
92
+ * Marks an abstract class as the root of an object graph. The Rust codegen
93
+ * emits a sibling `*.anvil.ts` file containing a concrete subclass that
94
+ * satisfies every abstract method using the configured modules.
95
+ *
96
+ * @example
97
+ * ```ts
98
+ * import { Component } from "@anvil-di/anvil";
99
+ * import { CoffeeModule } from "./coffee-module";
100
+ *
101
+ * @Component({ modules: [CoffeeModule] })
102
+ * export abstract class CoffeeShop {
103
+ * abstract pump(): Pump;
104
+ * }
105
+ * ```
106
+ */
107
+ export declare function Component(_config?: ComponentConfig): <T extends abstract new (...args: never[]) => unknown>(target: T, _ctx: ClassDecoratorContext<T>) => T;
108
+ /**
109
+ * Configuration for a `@Subcomponent`. Mirrors {@link ComponentConfig}.
110
+ */
111
+ export interface SubcomponentConfig {
112
+ /** Module classes whose `@Provides` methods are visible to this subcomponent. */
113
+ readonly modules?: ReadonlyArray<abstract new (...args: never[]) => unknown>;
114
+ }
115
+ /**
116
+ * Marks an abstract class as a child object graph nested inside a parent
117
+ * `@Component` (or another `@Subcomponent`). A subcomponent inherits all
118
+ * bindings declared by its parent and adds its own modules on top.
119
+ *
120
+ * The parent exposes the child by declaring an abstract zero-arg method
121
+ * whose return type is the subcomponent class; the codegen emits a
122
+ * factory that constructs `Dagger<Sub>` with the parent dagger as a
123
+ * back-reference, so inherited dependencies route through the parent's
124
+ * factories (and shared singletons stay shared).
125
+ *
126
+ * @example
127
+ * ```ts
128
+ * import { Component, Subcomponent } from "@anvil-di/anvil";
129
+ * import { RequestModule } from "./request-module";
130
+ *
131
+ * @Subcomponent({ modules: [RequestModule] })
132
+ * export abstract class RequestComponent {
133
+ * abstract handler(): RequestHandler;
134
+ * }
135
+ *
136
+ * @Component({ modules: [AppModule] })
137
+ * export abstract class AppComponent {
138
+ * abstract requestComponent(): RequestComponent;
139
+ * }
140
+ * ```
141
+ */
142
+ export declare function Subcomponent(_config?: SubcomponentConfig): <T extends abstract new (...args: never[]) => unknown>(target: T, _ctx: ClassDecoratorContext<T>) => T;
143
+ /**
144
+ * Declares that instances of the decorated class are cached for the lifetime
145
+ * of the owning component (i.e. one-per-component).
146
+ */
147
+ export declare function Singleton<This, Args extends readonly unknown[], Return>(target: (this: This, ...args: Args) => Return, ctx: ClassMethodDecoratorContext<This, (this: This, ...args: Args) => Return>): (this: This, ...args: Args) => Return;
148
+ export declare function Singleton<T extends abstract new (...args: never[]) => unknown>(target: T, ctx: ClassDecoratorContext<T>): T;
149
+ /**
150
+ * Marks an `@Provides` method as a contribution to a `Set<T>` multibinding,
151
+ * where `T` is the method's return type. The codegen aggregates every
152
+ * contribution to the same element type into a single factory that returns
153
+ * a `Set<T>` populated with each contributor's result.
154
+ *
155
+ * Multiple `@IntoSet` contributions to the same element type are not
156
+ * duplicates — they are intentionally collected. A consumer requests the
157
+ * aggregate by typing a parameter or entry-point return as `Set<T>`.
158
+ *
159
+ * Must be combined with `@Provides` (not `@Binds` or `@Inject`) for v0.1.
160
+ *
161
+ * @example
162
+ * ```ts
163
+ * import { Module, Provides, IntoSet, Component } from "@anvil-di/anvil";
164
+ *
165
+ * @Module
166
+ * export class PluginsModule {
167
+ * @Provides @IntoSet
168
+ * static auth(): Plugin { return new AuthPlugin(); }
169
+ *
170
+ * @Provides @IntoSet
171
+ * static logging(): Plugin { return new LoggingPlugin(); }
172
+ * }
173
+ *
174
+ * @Component({ modules: [PluginsModule] })
175
+ * export abstract class App {
176
+ * abstract plugins(): Set<Plugin>;
177
+ * }
178
+ * ```
179
+ */
180
+ export declare function IntoSet<This, Args extends readonly unknown[], Return>(target: (this: This, ...args: Args) => Return, _ctx: ClassMethodDecoratorContext<This, (this: This, ...args: Args) => Return>): (this: This, ...args: Args) => Return;
181
+ //# sourceMappingURL=decorators.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"decorators.d.ts","sourceRoot":"","sources":["../src/decorators.ts"],"names":[],"mappings":"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"}
@@ -0,0 +1,181 @@
1
+ /**
2
+ * No-op decorator stubs recognized by the `anvil` Rust codegen toolchain.
3
+ *
4
+ * These exist purely so user code typechecks (`import { Inject } from "@anvil-di/anvil"`).
5
+ * The actual semantics are implemented at compile time by reading the decorator
6
+ * AST and emitting wiring code into co-located `*.anvil.ts` files.
7
+ *
8
+ * All decorators are TC39 Stage-3 ([decision recorded in](../../../docs/adr/0002-stage3-decorators-only.md))
9
+ * and do not require `experimentalDecorators` in `tsconfig.json`.
10
+ */
11
+ /**
12
+ * Marks a class as a `@Module` whose static methods (annotated with `@Provides`)
13
+ * contribute bindings to a component's graph.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * import { Module, Provides } from "@anvil-di/anvil";
18
+ * import { Pump } from "./pump";
19
+ *
20
+ * @Module
21
+ * export class CoffeeModule {
22
+ * @Provides static providePump(p: Pump): Pump { return p; }
23
+ * }
24
+ * ```
25
+ */
26
+ export function Module(target, _ctx) {
27
+ return target;
28
+ }
29
+ /**
30
+ * Marks a static method on a `@Module` class as a binding factory.
31
+ *
32
+ * The method's return type is the bound key; its parameters are the dependencies.
33
+ * Must be `static` and have an explicit return type — both rules are enforced
34
+ * at codegen time.
35
+ */
36
+ export function Provides(target, _ctx) {
37
+ return target;
38
+ }
39
+ /**
40
+ * Marks a class for injection by the codegen toolchain. The class's
41
+ * constructor parameter types become the binding's dependencies.
42
+ *
43
+ * `@Inject` is a class-level decorator — TC39 Stage-3 decorators do not
44
+ * apply to constructors, so the legacy `@Inject constructor(...)` placement
45
+ * is rejected by the toolchain.
46
+ *
47
+ * @example
48
+ * ```ts
49
+ * import { Inject } from "@anvil-di/anvil";
50
+ * import { Heater } from "./heater";
51
+ *
52
+ * @Inject
53
+ * export class Pump {
54
+ * constructor(private heater: Heater) {}
55
+ * }
56
+ * ```
57
+ */
58
+ export function Inject(target, _ctx) {
59
+ return target;
60
+ }
61
+ /**
62
+ * Marks a static method on a `@Module` class as an alias-binding from
63
+ * the method's return type to its single parameter type. The codegen
64
+ * emits a factory that delegates straight to the parameter type's
65
+ * factory — useful for binding an interface (or abstract base class) to
66
+ * one concrete impl.
67
+ *
68
+ * The method must be `static`, take exactly one parameter, and declare
69
+ * an explicit return type. It must not also be `@Provides`. The body
70
+ * is required so the file type-checks, but the codegen ignores it and
71
+ * emits a delegate to the parameter's factory.
72
+ *
73
+ * (TC39 Stage-3 decorators cannot decorate `abstract` methods —
74
+ * TS error 1249 — which is why `@Binds` uses a static method with a
75
+ * trivial `return impl;` body rather than the Dagger-style abstract form.)
76
+ *
77
+ * @example
78
+ * ```ts
79
+ * import { Module, Binds } from "@anvil-di/anvil";
80
+ * import { Heater } from "./heater";
81
+ * import { ElectricHeater } from "./electric-heater";
82
+ *
83
+ * @Module
84
+ * export class CoffeeModule {
85
+ * @Binds static heater(impl: ElectricHeater): Heater { return impl; }
86
+ * }
87
+ * ```
88
+ */
89
+ export function Binds(target, _ctx) {
90
+ return target;
91
+ }
92
+ /**
93
+ * Marks an abstract class as the root of an object graph. The Rust codegen
94
+ * emits a sibling `*.anvil.ts` file containing a concrete subclass that
95
+ * satisfies every abstract method using the configured modules.
96
+ *
97
+ * @example
98
+ * ```ts
99
+ * import { Component } from "@anvil-di/anvil";
100
+ * import { CoffeeModule } from "./coffee-module";
101
+ *
102
+ * @Component({ modules: [CoffeeModule] })
103
+ * export abstract class CoffeeShop {
104
+ * abstract pump(): Pump;
105
+ * }
106
+ * ```
107
+ */
108
+ export function Component(_config = {}) {
109
+ return (target, _ctx) => target;
110
+ }
111
+ /**
112
+ * Marks an abstract class as a child object graph nested inside a parent
113
+ * `@Component` (or another `@Subcomponent`). A subcomponent inherits all
114
+ * bindings declared by its parent and adds its own modules on top.
115
+ *
116
+ * The parent exposes the child by declaring an abstract zero-arg method
117
+ * whose return type is the subcomponent class; the codegen emits a
118
+ * factory that constructs `Dagger<Sub>` with the parent dagger as a
119
+ * back-reference, so inherited dependencies route through the parent's
120
+ * factories (and shared singletons stay shared).
121
+ *
122
+ * @example
123
+ * ```ts
124
+ * import { Component, Subcomponent } from "@anvil-di/anvil";
125
+ * import { RequestModule } from "./request-module";
126
+ *
127
+ * @Subcomponent({ modules: [RequestModule] })
128
+ * export abstract class RequestComponent {
129
+ * abstract handler(): RequestHandler;
130
+ * }
131
+ *
132
+ * @Component({ modules: [AppModule] })
133
+ * export abstract class AppComponent {
134
+ * abstract requestComponent(): RequestComponent;
135
+ * }
136
+ * ```
137
+ */
138
+ export function Subcomponent(_config = {}) {
139
+ return (target, _ctx) => target;
140
+ }
141
+ // Implementation — both decorator shapes are no-ops at runtime; anvil's
142
+ // codegen reads the @Singleton presence from the AST and emits cache
143
+ // fields accordingly.
144
+ export function Singleton(target, _ctx) {
145
+ return target;
146
+ }
147
+ /**
148
+ * Marks an `@Provides` method as a contribution to a `Set<T>` multibinding,
149
+ * where `T` is the method's return type. The codegen aggregates every
150
+ * contribution to the same element type into a single factory that returns
151
+ * a `Set<T>` populated with each contributor's result.
152
+ *
153
+ * Multiple `@IntoSet` contributions to the same element type are not
154
+ * duplicates — they are intentionally collected. A consumer requests the
155
+ * aggregate by typing a parameter or entry-point return as `Set<T>`.
156
+ *
157
+ * Must be combined with `@Provides` (not `@Binds` or `@Inject`) for v0.1.
158
+ *
159
+ * @example
160
+ * ```ts
161
+ * import { Module, Provides, IntoSet, Component } from "@anvil-di/anvil";
162
+ *
163
+ * @Module
164
+ * export class PluginsModule {
165
+ * @Provides @IntoSet
166
+ * static auth(): Plugin { return new AuthPlugin(); }
167
+ *
168
+ * @Provides @IntoSet
169
+ * static logging(): Plugin { return new LoggingPlugin(); }
170
+ * }
171
+ *
172
+ * @Component({ modules: [PluginsModule] })
173
+ * export abstract class App {
174
+ * abstract plugins(): Set<Plugin>;
175
+ * }
176
+ * ```
177
+ */
178
+ export function IntoSet(target, _ctx) {
179
+ return target;
180
+ }
181
+ //# sourceMappingURL=decorators.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"decorators.js","sourceRoot":"","sources":["../src/decorators.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,MAAM,CACpB,MAAS,EACT,IAA8B;IAE9B,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,QAAQ,CACtB,MAA6C,EAC7C,IAA8E;IAE9E,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,MAAM,CACpB,MAAS,EACT,IAA8B;IAE9B,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,MAAM,UAAU,KAAK,CACnB,MAA6C,EAC7C,IAA8E;IAE9E,OAAO,MAAM,CAAC;AAChB,CAAC;AAUD;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,SAAS,CAAC,UAA2B,EAAE;IACrD,OAAO,CACL,MAAS,EACT,IAA8B,EAC3B,EAAE,CAAC,MAAM,CAAC;AACjB,CAAC;AAUD;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,UAAU,YAAY,CAAC,UAA8B,EAAE;IAC3D,OAAO,CACL,MAAS,EACT,IAA8B,EAC3B,EAAE,CAAC,MAAM,CAAC;AACjB,CAAC;AAgBD,wEAAwE;AACxE,qEAAqE;AACrE,sBAAsB;AACtB,MAAM,UAAU,SAAS,CAAC,MAAe,EAAE,IAAa;IACtD,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,MAAM,UAAU,OAAO,CACrB,MAA6C,EAC7C,IAA8E;IAE9E,OAAO,MAAM,CAAC;AAChB,CAAC"}
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Runtime stubs and types for the `anvil` compile-time dependency injection framework.
3
+ *
4
+ * Decorators exported from this package are intentionally no-ops at runtime —
5
+ * all real work happens at codegen time, performed by the `anvil` Rust CLI which
6
+ * reads decorated source and emits `*.anvil.ts` files containing the wired graph.
7
+ *
8
+ * See `docs/architecture.md` for the full pipeline.
9
+ */
10
+ export * from "./decorators.js";
11
+ export * from "./token.js";
12
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,cAAc,iBAAiB,CAAC;AAChC,cAAc,YAAY,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Runtime stubs and types for the `anvil` compile-time dependency injection framework.
3
+ *
4
+ * Decorators exported from this package are intentionally no-ops at runtime —
5
+ * all real work happens at codegen time, performed by the `anvil` Rust CLI which
6
+ * reads decorated source and emits `*.anvil.ts` files containing the wired graph.
7
+ *
8
+ * See `docs/architecture.md` for the full pipeline.
9
+ */
10
+ export * from "./decorators.js";
11
+ export * from "./token.js";
12
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,cAAc,iBAAiB,CAAC;AAChC,cAAc,YAAY,CAAC"}
@@ -0,0 +1,53 @@
1
+ /**
2
+ * `Token<T, Name>` is the type-annotation form for named binding keys.
3
+ *
4
+ * The codegen (M14+) recognizes `Token<T, "name">` as a type annotation in
5
+ * `@Provides` return types and `@Inject` constructor parameters and uses the
6
+ * string literal `"name"` as the binding's `Key::Token`. No runtime instance
7
+ * is needed — the name is read purely from the AST.
8
+ *
9
+ * The optional second type parameter `Name` carries the string-literal key
10
+ * so that two parameters annotated `Token<Database, "primary">` and
11
+ * `Token<Database, "replica">` are structurally distinct types that TypeScript
12
+ * will not treat as interchangeable.
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * // db-module.ts
17
+ * import { Module, Provides } from "@anvil-di/anvil";
18
+ * import type { Token } from "@anvil-di/anvil";
19
+ * import type { Database } from "./database";
20
+ *
21
+ * @Module
22
+ * export class DbModule {
23
+ * @Provides
24
+ * static primaryDb(): Token<Database, "primary-db"> {
25
+ * return new Database(primaryUrl) as unknown as Token<Database, "primary-db">;
26
+ * }
27
+ * }
28
+ *
29
+ * // app-component.ts
30
+ * import { Component, Inject } from "@anvil-di/anvil";
31
+ * import type { Token } from "@anvil-di/anvil";
32
+ * import type { Database } from "./database";
33
+ *
34
+ * @Inject
35
+ * export class UserRepository {
36
+ * constructor(private db: Token<Database, "primary-db">) {}
37
+ * }
38
+ * ```
39
+ */
40
+ export declare class Token<T, Name extends string = string> {
41
+ readonly description: string;
42
+ /** Phantom fields that fix the generic parameters and prevent structural collapse. */
43
+ private readonly _brand;
44
+ private readonly _name;
45
+ /**
46
+ * @param description Human-readable label, used in diagnostic output. Not
47
+ * required to be unique — the runtime identity is the `Token` instance.
48
+ */
49
+ constructor(description: string);
50
+ /** @returns the description, prefixed for easy log identification. */
51
+ toString(): string;
52
+ }
53
+ //# sourceMappingURL=token.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"token.d.ts","sourceRoot":"","sources":["../src/token.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AAEH,qBAAa,KAAK,CAAC,CAAC,EAAE,IAAI,SAAS,MAAM,GAAG,MAAM;aASpB,WAAW,EAAE,MAAM;IAR/C,sFAAsF;IACtF,iBAAyB,MAAM,CAAI;IACnC,iBAAyB,KAAK,CAAO;IAErC;;;OAGG;gBACyB,WAAW,EAAE,MAAM;IAE/C,sEAAsE;IACtE,QAAQ,IAAI,MAAM;CAGnB"}
package/dist/token.js ADDED
@@ -0,0 +1,55 @@
1
+ /**
2
+ * `Token<T, Name>` is the type-annotation form for named binding keys.
3
+ *
4
+ * The codegen (M14+) recognizes `Token<T, "name">` as a type annotation in
5
+ * `@Provides` return types and `@Inject` constructor parameters and uses the
6
+ * string literal `"name"` as the binding's `Key::Token`. No runtime instance
7
+ * is needed — the name is read purely from the AST.
8
+ *
9
+ * The optional second type parameter `Name` carries the string-literal key
10
+ * so that two parameters annotated `Token<Database, "primary">` and
11
+ * `Token<Database, "replica">` are structurally distinct types that TypeScript
12
+ * will not treat as interchangeable.
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * // db-module.ts
17
+ * import { Module, Provides } from "@anvil-di/anvil";
18
+ * import type { Token } from "@anvil-di/anvil";
19
+ * import type { Database } from "./database";
20
+ *
21
+ * @Module
22
+ * export class DbModule {
23
+ * @Provides
24
+ * static primaryDb(): Token<Database, "primary-db"> {
25
+ * return new Database(primaryUrl) as unknown as Token<Database, "primary-db">;
26
+ * }
27
+ * }
28
+ *
29
+ * // app-component.ts
30
+ * import { Component, Inject } from "@anvil-di/anvil";
31
+ * import type { Token } from "@anvil-di/anvil";
32
+ * import type { Database } from "./database";
33
+ *
34
+ * @Inject
35
+ * export class UserRepository {
36
+ * constructor(private db: Token<Database, "primary-db">) {}
37
+ * }
38
+ * ```
39
+ */
40
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
41
+ export class Token {
42
+ description;
43
+ /**
44
+ * @param description Human-readable label, used in diagnostic output. Not
45
+ * required to be unique — the runtime identity is the `Token` instance.
46
+ */
47
+ constructor(description) {
48
+ this.description = description;
49
+ }
50
+ /** @returns the description, prefixed for easy log identification. */
51
+ toString() {
52
+ return `Token<${this.description}>`;
53
+ }
54
+ }
55
+ //# sourceMappingURL=token.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"token.js","sourceRoot":"","sources":["../src/token.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,6DAA6D;AAC7D,MAAM,OAAO,KAAK;IASY;IAJ5B;;;OAGG;IACH,YAA4B,WAAmB;QAAnB,gBAAW,GAAX,WAAW,CAAQ;IAAG,CAAC;IAEnD,sEAAsE;IACtE,QAAQ;QACN,OAAO,SAAS,IAAI,CAAC,WAAW,GAAG,CAAC;IACtC,CAAC;CACF"}
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@anvil-di/anvil",
3
+ "version": "0.0.1",
4
+ "description": "Runtime stubs and types for the anvil compile-time dependency injection framework.",
5
+ "license": "Apache-2.0",
6
+ "type": "module",
7
+ "main": "./dist/index.js",
8
+ "module": "./dist/index.js",
9
+ "types": "./dist/index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "import": "./dist/index.js"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist",
18
+ "src"
19
+ ],
20
+ "devDependencies": {
21
+ "typescript": "^5.7.2",
22
+ "vitest": "^2.1.8"
23
+ },
24
+ "publishConfig": {
25
+ "access": "public"
26
+ },
27
+ "scripts": {
28
+ "build": "tsc -p tsconfig.json",
29
+ "typecheck": "tsc -p tsconfig.json --noEmit",
30
+ "test": "vitest run",
31
+ "lint": "tsc -p tsconfig.json --noEmit"
32
+ }
33
+ }
@@ -0,0 +1,239 @@
1
+ /**
2
+ * No-op decorator stubs recognized by the `anvil` Rust codegen toolchain.
3
+ *
4
+ * These exist purely so user code typechecks (`import { Inject } from "@anvil-di/anvil"`).
5
+ * The actual semantics are implemented at compile time by reading the decorator
6
+ * AST and emitting wiring code into co-located `*.anvil.ts` files.
7
+ *
8
+ * All decorators are TC39 Stage-3 ([decision recorded in](../../../docs/adr/0002-stage3-decorators-only.md))
9
+ * and do not require `experimentalDecorators` in `tsconfig.json`.
10
+ */
11
+
12
+ /**
13
+ * Marks a class as a `@Module` whose static methods (annotated with `@Provides`)
14
+ * contribute bindings to a component's graph.
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * import { Module, Provides } from "@anvil-di/anvil";
19
+ * import { Pump } from "./pump";
20
+ *
21
+ * @Module
22
+ * export class CoffeeModule {
23
+ * @Provides static providePump(p: Pump): Pump { return p; }
24
+ * }
25
+ * ```
26
+ */
27
+ export function Module<T extends abstract new (...args: never[]) => unknown>(
28
+ target: T,
29
+ _ctx: ClassDecoratorContext<T>,
30
+ ): T {
31
+ return target;
32
+ }
33
+
34
+ /**
35
+ * Marks a static method on a `@Module` class as a binding factory.
36
+ *
37
+ * The method's return type is the bound key; its parameters are the dependencies.
38
+ * Must be `static` and have an explicit return type — both rules are enforced
39
+ * at codegen time.
40
+ */
41
+ export function Provides<This, Args extends readonly unknown[], Return>(
42
+ target: (this: This, ...args: Args) => Return,
43
+ _ctx: ClassMethodDecoratorContext<This, (this: This, ...args: Args) => Return>,
44
+ ): (this: This, ...args: Args) => Return {
45
+ return target;
46
+ }
47
+
48
+ /**
49
+ * Marks a class for injection by the codegen toolchain. The class's
50
+ * constructor parameter types become the binding's dependencies.
51
+ *
52
+ * `@Inject` is a class-level decorator — TC39 Stage-3 decorators do not
53
+ * apply to constructors, so the legacy `@Inject constructor(...)` placement
54
+ * is rejected by the toolchain.
55
+ *
56
+ * @example
57
+ * ```ts
58
+ * import { Inject } from "@anvil-di/anvil";
59
+ * import { Heater } from "./heater";
60
+ *
61
+ * @Inject
62
+ * export class Pump {
63
+ * constructor(private heater: Heater) {}
64
+ * }
65
+ * ```
66
+ */
67
+ export function Inject<T extends abstract new (...args: never[]) => unknown>(
68
+ target: T,
69
+ _ctx: ClassDecoratorContext<T>,
70
+ ): T {
71
+ return target;
72
+ }
73
+
74
+ /**
75
+ * Marks a static method on a `@Module` class as an alias-binding from
76
+ * the method's return type to its single parameter type. The codegen
77
+ * emits a factory that delegates straight to the parameter type's
78
+ * factory — useful for binding an interface (or abstract base class) to
79
+ * one concrete impl.
80
+ *
81
+ * The method must be `static`, take exactly one parameter, and declare
82
+ * an explicit return type. It must not also be `@Provides`. The body
83
+ * is required so the file type-checks, but the codegen ignores it and
84
+ * emits a delegate to the parameter's factory.
85
+ *
86
+ * (TC39 Stage-3 decorators cannot decorate `abstract` methods —
87
+ * TS error 1249 — which is why `@Binds` uses a static method with a
88
+ * trivial `return impl;` body rather than the Dagger-style abstract form.)
89
+ *
90
+ * @example
91
+ * ```ts
92
+ * import { Module, Binds } from "@anvil-di/anvil";
93
+ * import { Heater } from "./heater";
94
+ * import { ElectricHeater } from "./electric-heater";
95
+ *
96
+ * @Module
97
+ * export class CoffeeModule {
98
+ * @Binds static heater(impl: ElectricHeater): Heater { return impl; }
99
+ * }
100
+ * ```
101
+ */
102
+ export function Binds<This, Args extends readonly unknown[], Return>(
103
+ target: (this: This, ...args: Args) => Return,
104
+ _ctx: ClassMethodDecoratorContext<This, (this: This, ...args: Args) => Return>,
105
+ ): (this: This, ...args: Args) => Return {
106
+ return target;
107
+ }
108
+
109
+ /**
110
+ * Configuration for a `@Component`.
111
+ */
112
+ export interface ComponentConfig {
113
+ /** Module classes whose `@Provides` methods are visible to this component. */
114
+ readonly modules?: ReadonlyArray<abstract new (...args: never[]) => unknown>;
115
+ }
116
+
117
+ /**
118
+ * Marks an abstract class as the root of an object graph. The Rust codegen
119
+ * emits a sibling `*.anvil.ts` file containing a concrete subclass that
120
+ * satisfies every abstract method using the configured modules.
121
+ *
122
+ * @example
123
+ * ```ts
124
+ * import { Component } from "@anvil-di/anvil";
125
+ * import { CoffeeModule } from "./coffee-module";
126
+ *
127
+ * @Component({ modules: [CoffeeModule] })
128
+ * export abstract class CoffeeShop {
129
+ * abstract pump(): Pump;
130
+ * }
131
+ * ```
132
+ */
133
+ export function Component(_config: ComponentConfig = {}) {
134
+ return <T extends abstract new (...args: never[]) => unknown>(
135
+ target: T,
136
+ _ctx: ClassDecoratorContext<T>,
137
+ ): T => target;
138
+ }
139
+
140
+ /**
141
+ * Configuration for a `@Subcomponent`. Mirrors {@link ComponentConfig}.
142
+ */
143
+ export interface SubcomponentConfig {
144
+ /** Module classes whose `@Provides` methods are visible to this subcomponent. */
145
+ readonly modules?: ReadonlyArray<abstract new (...args: never[]) => unknown>;
146
+ }
147
+
148
+ /**
149
+ * Marks an abstract class as a child object graph nested inside a parent
150
+ * `@Component` (or another `@Subcomponent`). A subcomponent inherits all
151
+ * bindings declared by its parent and adds its own modules on top.
152
+ *
153
+ * The parent exposes the child by declaring an abstract zero-arg method
154
+ * whose return type is the subcomponent class; the codegen emits a
155
+ * factory that constructs `Dagger<Sub>` with the parent dagger as a
156
+ * back-reference, so inherited dependencies route through the parent's
157
+ * factories (and shared singletons stay shared).
158
+ *
159
+ * @example
160
+ * ```ts
161
+ * import { Component, Subcomponent } from "@anvil-di/anvil";
162
+ * import { RequestModule } from "./request-module";
163
+ *
164
+ * @Subcomponent({ modules: [RequestModule] })
165
+ * export abstract class RequestComponent {
166
+ * abstract handler(): RequestHandler;
167
+ * }
168
+ *
169
+ * @Component({ modules: [AppModule] })
170
+ * export abstract class AppComponent {
171
+ * abstract requestComponent(): RequestComponent;
172
+ * }
173
+ * ```
174
+ */
175
+ export function Subcomponent(_config: SubcomponentConfig = {}) {
176
+ return <T extends abstract new (...args: never[]) => unknown>(
177
+ target: T,
178
+ _ctx: ClassDecoratorContext<T>,
179
+ ): T => target;
180
+ }
181
+
182
+ /**
183
+ * Declares that instances of the decorated class are cached for the lifetime
184
+ * of the owning component (i.e. one-per-component).
185
+ */
186
+ // Method-decorator overload: `@Singleton @Provides static fn(...): T`.
187
+ export function Singleton<This, Args extends readonly unknown[], Return>(
188
+ target: (this: This, ...args: Args) => Return,
189
+ ctx: ClassMethodDecoratorContext<This, (this: This, ...args: Args) => Return>,
190
+ ): (this: This, ...args: Args) => Return;
191
+ // Class-decorator overload: `@Inject @Singleton class X {}`.
192
+ export function Singleton<T extends abstract new (...args: never[]) => unknown>(
193
+ target: T,
194
+ ctx: ClassDecoratorContext<T>,
195
+ ): T;
196
+ // Implementation — both decorator shapes are no-ops at runtime; anvil's
197
+ // codegen reads the @Singleton presence from the AST and emits cache
198
+ // fields accordingly.
199
+ export function Singleton(target: unknown, _ctx: unknown): unknown {
200
+ return target;
201
+ }
202
+
203
+ /**
204
+ * Marks an `@Provides` method as a contribution to a `Set<T>` multibinding,
205
+ * where `T` is the method's return type. The codegen aggregates every
206
+ * contribution to the same element type into a single factory that returns
207
+ * a `Set<T>` populated with each contributor's result.
208
+ *
209
+ * Multiple `@IntoSet` contributions to the same element type are not
210
+ * duplicates — they are intentionally collected. A consumer requests the
211
+ * aggregate by typing a parameter or entry-point return as `Set<T>`.
212
+ *
213
+ * Must be combined with `@Provides` (not `@Binds` or `@Inject`) for v0.1.
214
+ *
215
+ * @example
216
+ * ```ts
217
+ * import { Module, Provides, IntoSet, Component } from "@anvil-di/anvil";
218
+ *
219
+ * @Module
220
+ * export class PluginsModule {
221
+ * @Provides @IntoSet
222
+ * static auth(): Plugin { return new AuthPlugin(); }
223
+ *
224
+ * @Provides @IntoSet
225
+ * static logging(): Plugin { return new LoggingPlugin(); }
226
+ * }
227
+ *
228
+ * @Component({ modules: [PluginsModule] })
229
+ * export abstract class App {
230
+ * abstract plugins(): Set<Plugin>;
231
+ * }
232
+ * ```
233
+ */
234
+ export function IntoSet<This, Args extends readonly unknown[], Return>(
235
+ target: (this: This, ...args: Args) => Return,
236
+ _ctx: ClassMethodDecoratorContext<This, (this: This, ...args: Args) => Return>,
237
+ ): (this: This, ...args: Args) => Return {
238
+ return target;
239
+ }
package/src/index.ts ADDED
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Runtime stubs and types for the `anvil` compile-time dependency injection framework.
3
+ *
4
+ * Decorators exported from this package are intentionally no-ops at runtime —
5
+ * all real work happens at codegen time, performed by the `anvil` Rust CLI which
6
+ * reads decorated source and emits `*.anvil.ts` files containing the wired graph.
7
+ *
8
+ * See `docs/architecture.md` for the full pipeline.
9
+ */
10
+
11
+ export * from "./decorators.js";
12
+ export * from "./token.js";
@@ -0,0 +1,21 @@
1
+ import { describe, expect, it } from "vitest";
2
+
3
+ import { Token } from "./token.js";
4
+
5
+ describe("Token", () => {
6
+ it("retains its description", () => {
7
+ const t = new Token<string>("LOGGER");
8
+ expect(t.description).toBe("LOGGER");
9
+ });
10
+
11
+ it("renders identifiably in toString", () => {
12
+ const t = new Token<number>("MAX_RETRIES");
13
+ expect(`${t}`).toBe("Token<MAX_RETRIES>");
14
+ });
15
+
16
+ it("two tokens with the same description are not equal by identity", () => {
17
+ const a = new Token<string>("X");
18
+ const b = new Token<string>("X");
19
+ expect(a).not.toBe(b);
20
+ });
21
+ });
package/src/token.ts ADDED
@@ -0,0 +1,56 @@
1
+ /**
2
+ * `Token<T, Name>` is the type-annotation form for named binding keys.
3
+ *
4
+ * The codegen (M14+) recognizes `Token<T, "name">` as a type annotation in
5
+ * `@Provides` return types and `@Inject` constructor parameters and uses the
6
+ * string literal `"name"` as the binding's `Key::Token`. No runtime instance
7
+ * is needed — the name is read purely from the AST.
8
+ *
9
+ * The optional second type parameter `Name` carries the string-literal key
10
+ * so that two parameters annotated `Token<Database, "primary">` and
11
+ * `Token<Database, "replica">` are structurally distinct types that TypeScript
12
+ * will not treat as interchangeable.
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * // db-module.ts
17
+ * import { Module, Provides } from "@anvil-di/anvil";
18
+ * import type { Token } from "@anvil-di/anvil";
19
+ * import type { Database } from "./database";
20
+ *
21
+ * @Module
22
+ * export class DbModule {
23
+ * @Provides
24
+ * static primaryDb(): Token<Database, "primary-db"> {
25
+ * return new Database(primaryUrl) as unknown as Token<Database, "primary-db">;
26
+ * }
27
+ * }
28
+ *
29
+ * // app-component.ts
30
+ * import { Component, Inject } from "@anvil-di/anvil";
31
+ * import type { Token } from "@anvil-di/anvil";
32
+ * import type { Database } from "./database";
33
+ *
34
+ * @Inject
35
+ * export class UserRepository {
36
+ * constructor(private db: Token<Database, "primary-db">) {}
37
+ * }
38
+ * ```
39
+ */
40
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
41
+ export class Token<T, Name extends string = string> {
42
+ /** Phantom fields that fix the generic parameters and prevent structural collapse. */
43
+ declare private readonly _brand: T;
44
+ declare private readonly _name: Name;
45
+
46
+ /**
47
+ * @param description Human-readable label, used in diagnostic output. Not
48
+ * required to be unique — the runtime identity is the `Token` instance.
49
+ */
50
+ constructor(public readonly description: string) {}
51
+
52
+ /** @returns the description, prefixed for easy log identification. */
53
+ toString(): string {
54
+ return `Token<${this.description}>`;
55
+ }
56
+ }