strict-ts-lib-v5.9-branded-decorators 0.2.0

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/index.d.ts ADDED
@@ -0,0 +1,362 @@
1
+ /// <reference no-default-lib="true"/>
2
+
3
+ /**
4
+ * The decorator context types provided to class element decorators.
5
+ */
6
+ type ClassMemberDecoratorContext =
7
+ | ClassMethodDecoratorContext
8
+ | ClassGetterDecoratorContext
9
+ | ClassSetterDecoratorContext
10
+ | ClassFieldDecoratorContext
11
+ | ClassAccessorDecoratorContext;
12
+
13
+ /**
14
+ * The decorator context types provided to any decorator.
15
+ */
16
+ type DecoratorContext = ClassDecoratorContext | ClassMemberDecoratorContext;
17
+
18
+ type DecoratorMetadataObject = Record<PropertyKey, unknown> & object;
19
+
20
+ type DecoratorMetadata = typeof globalThis extends {
21
+ readonly Symbol: { readonly metadata: symbol };
22
+ }
23
+ ? DecoratorMetadataObject
24
+ : DecoratorMetadataObject | undefined;
25
+
26
+ /**
27
+ * Context provided to a class decorator.
28
+ * @template Class The type of the decorated class associated with this context.
29
+ */
30
+ interface ClassDecoratorContext<
31
+ Class extends abstract new (...args: readonly never[]) => unknown =
32
+ abstract new (...args: readonly never[]) => unknown,
33
+ > {
34
+ /** The kind of element that was decorated. */
35
+ readonly kind: 'class';
36
+
37
+ /** The name of the decorated class. */
38
+ readonly name: string | undefined;
39
+
40
+ /**
41
+ * Adds a callback to be invoked after the class definition has been finalized.
42
+ *
43
+ * @example
44
+ * ```ts
45
+ * function customElement(name: string): ClassDecoratorFunction {
46
+ * return (target, context) => {
47
+ * context.addInitializer(function () {
48
+ * customElements.define(name, this);
49
+ * });
50
+ * }
51
+ * }
52
+ *
53
+ * @customElement("my-element")
54
+ * class MyElement {}
55
+ * ```
56
+ */
57
+ addInitializer(initializer: (this: Class) => void): void;
58
+
59
+ readonly metadata: DecoratorMetadata;
60
+ }
61
+
62
+ /**
63
+ * Context provided to a class method decorator.
64
+ * @template This The type on which the class element will be defined. For a static class element, this will be
65
+ * the type of the constructor. For a non-static class element, this will be the type of the instance.
66
+ * @template Value The type of the decorated class method.
67
+ */
68
+ interface ClassMethodDecoratorContext<
69
+ This = unknown,
70
+ Value extends (this: This, ...args: readonly never[]) => unknown = (
71
+ this: This,
72
+ ...args: readonly never[]
73
+ ) => unknown,
74
+ > {
75
+ /** The kind of class element that was decorated. */
76
+ readonly kind: 'method';
77
+
78
+ /** The name of the decorated class element. */
79
+ readonly name: string | symbol;
80
+
81
+ /** A value indicating whether the class element is a static (`true`) or instance (`false`) element. */
82
+ readonly static: boolean;
83
+
84
+ /** A value indicating whether the class element has a private name. */
85
+ readonly private: boolean;
86
+
87
+ /** An object that can be used to access the current value of the class element at runtime. */
88
+ readonly access: {
89
+ /**
90
+ * Determines whether an object has a property with the same name as the decorated element.
91
+ */
92
+ has(object: This): boolean;
93
+ /**
94
+ * Gets the current value of the method from the provided object.
95
+ *
96
+ * @example
97
+ * let fn = context.access.get(instance);
98
+ */
99
+ get(object: This): Value;
100
+ };
101
+
102
+ /**
103
+ * Adds a callback to be invoked either after static methods are defined but before
104
+ * static initializers are run (when decorating a `static` element), or before instance
105
+ * initializers are run (when decorating a non-`static` element).
106
+ *
107
+ * @example
108
+ * ```ts
109
+ * const bound: ClassMethodDecoratorFunction = (value, context) {
110
+ * if (context.private) throw new TypeError("Not supported on private methods.");
111
+ * context.addInitializer(function () {
112
+ * this[context.name] = this[context.name].bind(this);
113
+ * });
114
+ * }
115
+ *
116
+ * class C {
117
+ * message = "Hello";
118
+ *
119
+ * @bound
120
+ * m() {
121
+ * console.log(this.message);
122
+ * }
123
+ * }
124
+ * ```
125
+ */
126
+ addInitializer(initializer: (this: This) => void): void;
127
+
128
+ readonly metadata: DecoratorMetadata;
129
+ }
130
+
131
+ /**
132
+ * Context provided to a class getter decorator.
133
+ * @template This The type on which the class element will be defined. For a static class element, this will be
134
+ * the type of the constructor. For a non-static class element, this will be the type of the instance.
135
+ * @template Value The property type of the decorated class getter.
136
+ */
137
+ interface ClassGetterDecoratorContext<This = unknown, Value = unknown> {
138
+ /** The kind of class element that was decorated. */
139
+ readonly kind: 'getter';
140
+
141
+ /** The name of the decorated class element. */
142
+ readonly name: string | symbol;
143
+
144
+ /** A value indicating whether the class element is a static (`true`) or instance (`false`) element. */
145
+ readonly static: boolean;
146
+
147
+ /** A value indicating whether the class element has a private name. */
148
+ readonly private: boolean;
149
+
150
+ /** An object that can be used to access the current value of the class element at runtime. */
151
+ readonly access: {
152
+ /**
153
+ * Determines whether an object has a property with the same name as the decorated element.
154
+ */
155
+ has(object: This): boolean;
156
+ /**
157
+ * Invokes the getter on the provided object.
158
+ *
159
+ * @example
160
+ * let value = context.access.get(instance);
161
+ */
162
+ get(object: This): Value;
163
+ };
164
+
165
+ /**
166
+ * Adds a callback to be invoked either after static methods are defined but before
167
+ * static initializers are run (when decorating a `static` element), or before instance
168
+ * initializers are run (when decorating a non-`static` element).
169
+ */
170
+ addInitializer(initializer: (this: This) => void): void;
171
+
172
+ readonly metadata: DecoratorMetadata;
173
+ }
174
+
175
+ /**
176
+ * Context provided to a class setter decorator.
177
+ * @template This The type on which the class element will be defined. For a static class element, this will be
178
+ * the type of the constructor. For a non-static class element, this will be the type of the instance.
179
+ * @template Value The type of the decorated class setter.
180
+ */
181
+ interface ClassSetterDecoratorContext<This = unknown, Value = unknown> {
182
+ /** The kind of class element that was decorated. */
183
+ readonly kind: 'setter';
184
+
185
+ /** The name of the decorated class element. */
186
+ readonly name: string | symbol;
187
+
188
+ /** A value indicating whether the class element is a static (`true`) or instance (`false`) element. */
189
+ readonly static: boolean;
190
+
191
+ /** A value indicating whether the class element has a private name. */
192
+ readonly private: boolean;
193
+
194
+ /** An object that can be used to access the current value of the class element at runtime. */
195
+ readonly access: {
196
+ /**
197
+ * Determines whether an object has a property with the same name as the decorated element.
198
+ */
199
+ has(object: This): boolean;
200
+ /**
201
+ * Invokes the setter on the provided object.
202
+ *
203
+ * @example
204
+ * context.access.set(instance, value);
205
+ */
206
+ set(object: This, value: Value): void;
207
+ };
208
+
209
+ /**
210
+ * Adds a callback to be invoked either after static methods are defined but before
211
+ * static initializers are run (when decorating a `static` element), or before instance
212
+ * initializers are run (when decorating a non-`static` element).
213
+ */
214
+ addInitializer(initializer: (this: This) => void): void;
215
+
216
+ readonly metadata: DecoratorMetadata;
217
+ }
218
+
219
+ /**
220
+ * Context provided to a class `accessor` field decorator.
221
+ * @template This The type on which the class element will be defined. For a static class element, this will be
222
+ * the type of the constructor. For a non-static class element, this will be the type of the instance.
223
+ * @template Value The type of decorated class field.
224
+ */
225
+ interface ClassAccessorDecoratorContext<This = unknown, Value = unknown> {
226
+ /** The kind of class element that was decorated. */
227
+ readonly kind: 'accessor';
228
+
229
+ /** The name of the decorated class element. */
230
+ readonly name: string | symbol;
231
+
232
+ /** A value indicating whether the class element is a static (`true`) or instance (`false`) element. */
233
+ readonly static: boolean;
234
+
235
+ /** A value indicating whether the class element has a private name. */
236
+ readonly private: boolean;
237
+
238
+ /** An object that can be used to access the current value of the class element at runtime. */
239
+ readonly access: {
240
+ /**
241
+ * Determines whether an object has a property with the same name as the decorated element.
242
+ */
243
+ has(object: This): boolean;
244
+
245
+ /**
246
+ * Invokes the getter on the provided object.
247
+ *
248
+ * @example
249
+ * let value = context.access.get(instance);
250
+ */
251
+ get(object: This): Value;
252
+
253
+ /**
254
+ * Invokes the setter on the provided object.
255
+ *
256
+ * @example
257
+ * context.access.set(instance, value);
258
+ */
259
+ set(object: This, value: Value): void;
260
+ };
261
+
262
+ /**
263
+ * Adds a callback to be invoked immediately after the auto `accessor` being
264
+ * decorated is initialized (regardless if the `accessor` is `static` or not).
265
+ */
266
+ addInitializer(initializer: (this: This) => void): void;
267
+
268
+ readonly metadata: DecoratorMetadata;
269
+ }
270
+
271
+ /**
272
+ * Describes the target provided to class `accessor` field decorators.
273
+ * @template This The `this` type to which the target applies.
274
+ * @template Value The property type for the class `accessor` field.
275
+ */
276
+ interface ClassAccessorDecoratorTarget<This, Value> {
277
+ /**
278
+ * Invokes the getter that was defined prior to decorator application.
279
+ *
280
+ * @example
281
+ * let value = target.get.call(instance);
282
+ */
283
+ get(this: This): Value;
284
+
285
+ /**
286
+ * Invokes the setter that was defined prior to decorator application.
287
+ *
288
+ * @example
289
+ * target.set.call(instance, value);
290
+ */
291
+ set(this: This, value: Value): void;
292
+ }
293
+
294
+ /**
295
+ * Describes the allowed return value from a class `accessor` field decorator.
296
+ * @template This The `this` type to which the target applies.
297
+ * @template Value The property type for the class `accessor` field.
298
+ */
299
+ interface ClassAccessorDecoratorResult<This, Value> {
300
+ /**
301
+ * An optional replacement getter function. If not provided, the existing getter function is used instead.
302
+ */
303
+ get?(this: This): Value;
304
+
305
+ /**
306
+ * An optional replacement setter function. If not provided, the existing setter function is used instead.
307
+ */
308
+ set?(this: This, value: Value): void;
309
+
310
+ /**
311
+ * An optional initializer mutator that is invoked when the underlying field initializer is evaluated.
312
+ * @param value The incoming initializer value.
313
+ * @returns The replacement initializer value.
314
+ */
315
+ init?(this: This, value: Value): Value;
316
+ }
317
+
318
+ /**
319
+ * Context provided to a class field decorator.
320
+ * @template This The type on which the class element will be defined. For a static class element, this will be
321
+ * the type of the constructor. For a non-static class element, this will be the type of the instance.
322
+ * @template Value The type of the decorated class field.
323
+ */
324
+ interface ClassFieldDecoratorContext<This = unknown, Value = unknown> {
325
+ /** The kind of class element that was decorated. */
326
+ readonly kind: 'field';
327
+
328
+ /** The name of the decorated class element. */
329
+ readonly name: string | symbol;
330
+
331
+ /** A value indicating whether the class element is a static (`true`) or instance (`false`) element. */
332
+ readonly static: boolean;
333
+
334
+ /** A value indicating whether the class element has a private name. */
335
+ readonly private: boolean;
336
+
337
+ /** An object that can be used to access the current value of the class element at runtime. */
338
+ readonly access: {
339
+ /**
340
+ * Determines whether an object has a property with the same name as the decorated element.
341
+ */
342
+ has(object: This): boolean;
343
+
344
+ /**
345
+ * Gets the value of the field on the provided object.
346
+ */
347
+ get(object: This): Value;
348
+
349
+ /**
350
+ * Sets the value of the field on the provided object.
351
+ */
352
+ set(object: This, value: Value): void;
353
+ };
354
+
355
+ /**
356
+ * Adds a callback to be invoked immediately after the field being decorated
357
+ * is initialized (regardless if the field is `static` or not).
358
+ */
359
+ addInitializer(initializer: (this: This) => void): void;
360
+
361
+ readonly metadata: DecoratorMetadata;
362
+ }
@@ -0,0 +1,19 @@
1
+ /// <reference no-default-lib="true"/>
2
+
3
+ declare type ClassDecorator = <TFunction extends Function>(
4
+ target: TFunction,
5
+ ) => TFunction | void;
6
+ declare type PropertyDecorator = (
7
+ target: Object,
8
+ propertyKey: string | symbol,
9
+ ) => void;
10
+ declare type MethodDecorator = <T>(
11
+ target: Object,
12
+ propertyKey: string | symbol,
13
+ descriptor: TypedPropertyDescriptor<T>,
14
+ ) => TypedPropertyDescriptor<T> | void;
15
+ declare type ParameterDecorator = (
16
+ target: Object,
17
+ propertyKey: string | symbol | undefined,
18
+ parameterIndex: number,
19
+ ) => void;
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "strict-ts-lib-v5.9-branded-decorators-legacy",
3
+ "version": "0.2.0",
4
+ "private": false,
5
+ "description": "Strict TypeScript lib",
6
+ "license": "Apache-2.0",
7
+ "author": "noshiro-pf <noshiro.pf@gmail.com>",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/noshiro-pf/strict-typescript-lib.git"
11
+ },
12
+ "type": "module",
13
+ "sideEffects": false,
14
+ "types": "./index.d.ts",
15
+ "dependencies": {
16
+ "ts-type-forge": "^7.0.0"
17
+ },
18
+ "peerDependencies": {
19
+ "typescript": ">=5.9.0 <6.0.0"
20
+ }
21
+ }
package/package.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "strict-ts-lib-v5.9-branded-decorators",
3
+ "version": "0.2.0",
4
+ "private": false,
5
+ "description": "Strict TypeScript lib",
6
+ "license": "Apache-2.0",
7
+ "author": "noshiro-pf <noshiro.pf@gmail.com>",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/noshiro-pf/strict-typescript-lib.git"
11
+ },
12
+ "type": "module",
13
+ "sideEffects": false,
14
+ "types": "./index.d.ts",
15
+ "dependencies": {
16
+ "ts-type-forge": "^7.0.0"
17
+ },
18
+ "peerDependencies": {
19
+ "typescript": ">=5.9.0 <6.0.0"
20
+ }
21
+ }