@fluidframework/type-factory 2.93.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.
Files changed (64) hide show
  1. package/.mocharc.cjs +11 -0
  2. package/CHANGELOG.md +3 -0
  3. package/LICENSE +21 -0
  4. package/README.md +133 -0
  5. package/alpha.d.ts +11 -0
  6. package/api-extractor/api-extractor-lint-alpha.cjs.json +5 -0
  7. package/api-extractor/api-extractor-lint-alpha.esm.json +5 -0
  8. package/api-extractor/api-extractor-lint-bundle.json +5 -0
  9. package/api-extractor/api-extractor-lint-index.cjs.json +5 -0
  10. package/api-extractor/api-extractor-lint-index.esm.json +5 -0
  11. package/api-extractor/api-extractor-lint-public.cjs.json +5 -0
  12. package/api-extractor/api-extractor-lint-public.esm.json +5 -0
  13. package/api-extractor.json +14 -0
  14. package/api-report/type-factory.alpha.api.md +208 -0
  15. package/api-report/type-factory.beta.api.md +7 -0
  16. package/api-report/type-factory.public.api.md +7 -0
  17. package/dist/alpha.d.ts +57 -0
  18. package/dist/index.d.ts +14 -0
  19. package/dist/index.d.ts.map +1 -0
  20. package/dist/index.js +21 -0
  21. package/dist/index.js.map +1 -0
  22. package/dist/methodBinding.d.ts +101 -0
  23. package/dist/methodBinding.d.ts.map +1 -0
  24. package/dist/methodBinding.js +26 -0
  25. package/dist/methodBinding.js.map +1 -0
  26. package/dist/package.json +4 -0
  27. package/dist/propertyBinding.d.ts +50 -0
  28. package/dist/propertyBinding.d.ts.map +1 -0
  29. package/dist/propertyBinding.js +13 -0
  30. package/dist/propertyBinding.js.map +1 -0
  31. package/dist/public.d.ts +16 -0
  32. package/dist/treeAgentTypes.d.ts +428 -0
  33. package/dist/treeAgentTypes.d.ts.map +1 -0
  34. package/dist/treeAgentTypes.js +218 -0
  35. package/dist/treeAgentTypes.js.map +1 -0
  36. package/eslint.config.mts +43 -0
  37. package/internal.d.ts +11 -0
  38. package/lib/alpha.d.ts +57 -0
  39. package/lib/index.d.ts +14 -0
  40. package/lib/index.d.ts.map +1 -0
  41. package/lib/index.js +13 -0
  42. package/lib/index.js.map +1 -0
  43. package/lib/methodBinding.d.ts +101 -0
  44. package/lib/methodBinding.d.ts.map +1 -0
  45. package/lib/methodBinding.js +22 -0
  46. package/lib/methodBinding.js.map +1 -0
  47. package/lib/propertyBinding.d.ts +50 -0
  48. package/lib/propertyBinding.d.ts.map +1 -0
  49. package/lib/propertyBinding.js +10 -0
  50. package/lib/propertyBinding.js.map +1 -0
  51. package/lib/public.d.ts +16 -0
  52. package/lib/treeAgentTypes.d.ts +428 -0
  53. package/lib/treeAgentTypes.d.ts.map +1 -0
  54. package/lib/treeAgentTypes.js +214 -0
  55. package/lib/treeAgentTypes.js.map +1 -0
  56. package/lib/tsdoc-metadata.json +11 -0
  57. package/package.json +142 -0
  58. package/src/index.ts +57 -0
  59. package/src/methodBinding.ts +139 -0
  60. package/src/propertyBinding.ts +62 -0
  61. package/src/treeAgentTypes.ts +601 -0
  62. package/tsconfig.cjs.json +7 -0
  63. package/tsconfig.json +14 -0
  64. package/tsdoc.json +4 -0
@@ -0,0 +1,601 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { UsageError } from "@fluidframework/telemetry-utils/internal";
7
+
8
+ /**
9
+ * Type kinds for the type factory type system.
10
+ * @alpha
11
+ */
12
+ export type TypeFactoryTypeKind =
13
+ | "string"
14
+ | "number"
15
+ | "boolean"
16
+ | "void"
17
+ | "undefined"
18
+ | "null"
19
+ | "unknown"
20
+ | "date"
21
+ | "promise"
22
+ | "array"
23
+ | "object"
24
+ | "record"
25
+ | "map"
26
+ | "tuple"
27
+ | "union"
28
+ | "intersection"
29
+ | "literal"
30
+ | "optional"
31
+ | "readonly"
32
+ | "function"
33
+ | "instanceof";
34
+
35
+ /**
36
+ * Base interface for type factory types.
37
+ * @alpha
38
+ */
39
+ export interface TypeFactoryType {
40
+ /**
41
+ * The kind of type this represents.
42
+ */
43
+ readonly _kind: TypeFactoryTypeKind;
44
+ }
45
+
46
+ /**
47
+ * Set of valid type factory type kinds for efficient validation.
48
+ * @internal
49
+ */
50
+ const validTypeKinds: ReadonlySet<TypeFactoryTypeKind> = new Set<TypeFactoryTypeKind>([
51
+ "string",
52
+ "number",
53
+ "boolean",
54
+ "void",
55
+ "undefined",
56
+ "null",
57
+ "unknown",
58
+ "date",
59
+ "promise",
60
+ "array",
61
+ "object",
62
+ "record",
63
+ "map",
64
+ "tuple",
65
+ "union",
66
+ "intersection",
67
+ "literal",
68
+ "optional",
69
+ "readonly",
70
+ "function",
71
+ "instanceof",
72
+ ]);
73
+
74
+ /**
75
+ * Type guard to check if a value is a type factory type.
76
+ * @alpha
77
+ */
78
+ export function isTypeFactoryType(value: unknown): value is TypeFactoryType {
79
+ if (typeof value !== "object" || value === null || !("_kind" in value)) {
80
+ return false;
81
+ }
82
+ const kind = (value as { _kind: unknown })._kind;
83
+ return typeof kind === "string" && validTypeKinds.has(kind as TypeFactoryTypeKind);
84
+ }
85
+
86
+ // Primitive type factories
87
+
88
+ /**
89
+ * Represents a string type in the type factory system.
90
+ * @alpha
91
+ */
92
+ export interface TypeFactoryString extends TypeFactoryType {
93
+ /**
94
+ * {@inheritDoc TypeFactoryType._kind}
95
+ */
96
+ readonly _kind: "string";
97
+ }
98
+
99
+ /**
100
+ * Represents a number type in the type factory system.
101
+ * @alpha
102
+ */
103
+ export interface TypeFactoryNumber extends TypeFactoryType {
104
+ /**
105
+ * {@inheritDoc TypeFactoryType._kind}
106
+ */
107
+ readonly _kind: "number";
108
+ }
109
+
110
+ /**
111
+ * Represents a boolean type in the type factory system.
112
+ * @alpha
113
+ */
114
+ export interface TypeFactoryBoolean extends TypeFactoryType {
115
+ /**
116
+ * {@inheritDoc TypeFactoryType._kind}
117
+ */
118
+ readonly _kind: "boolean";
119
+ }
120
+
121
+ /**
122
+ * Represents a Date type in the type factory system.
123
+ * @alpha
124
+ */
125
+ export interface TypeFactoryDate extends TypeFactoryType {
126
+ /**
127
+ * {@inheritDoc TypeFactoryType._kind}
128
+ */
129
+ readonly _kind: "date";
130
+ }
131
+
132
+ /**
133
+ * Represents a void type in the type factory system.
134
+ * @alpha
135
+ */
136
+ export interface TypeFactoryVoid extends TypeFactoryType {
137
+ /**
138
+ * {@inheritDoc TypeFactoryType._kind}
139
+ */
140
+ readonly _kind: "void";
141
+ }
142
+
143
+ /**
144
+ * Represents an undefined type in the type factory system.
145
+ * @alpha
146
+ */
147
+ export interface TypeFactoryUndefined extends TypeFactoryType {
148
+ /**
149
+ * {@inheritDoc TypeFactoryType._kind}
150
+ */
151
+ readonly _kind: "undefined";
152
+ }
153
+
154
+ /**
155
+ * Represents a null type in the type factory system.
156
+ * @alpha
157
+ */
158
+ export interface TypeFactoryNull extends TypeFactoryType {
159
+ /**
160
+ * {@inheritDoc TypeFactoryType._kind}
161
+ */
162
+ readonly _kind: "null";
163
+ }
164
+
165
+ /**
166
+ * Represents an unknown type in the type factory system.
167
+ * @alpha
168
+ */
169
+ export interface TypeFactoryUnknown extends TypeFactoryType {
170
+ /**
171
+ * {@inheritDoc TypeFactoryType._kind}
172
+ */
173
+ readonly _kind: "unknown";
174
+ }
175
+
176
+ // Complex type interfaces
177
+
178
+ /**
179
+ * Represents an array type in the type factory system.
180
+ * @alpha
181
+ */
182
+ export interface TypeFactoryArray extends TypeFactoryType {
183
+ /**
184
+ * {@inheritDoc TypeFactoryType._kind}
185
+ */
186
+ readonly _kind: "array";
187
+ /**
188
+ * The type of elements in the array.
189
+ */
190
+ readonly element: TypeFactoryType;
191
+ }
192
+
193
+ /**
194
+ * Represents a Promise type in the type factory system.
195
+ * @alpha
196
+ */
197
+ export interface TypeFactoryPromise extends TypeFactoryType {
198
+ /**
199
+ * {@inheritDoc TypeFactoryType._kind}
200
+ */
201
+ readonly _kind: "promise";
202
+ /**
203
+ * The type that the Promise resolves to.
204
+ */
205
+ readonly innerType: TypeFactoryType;
206
+ }
207
+
208
+ /**
209
+ * Represents an object type with a fixed shape in the type factory system.
210
+ * @alpha
211
+ */
212
+ export interface TypeFactoryObject extends TypeFactoryType {
213
+ /**
214
+ * {@inheritDoc TypeFactoryType._kind}
215
+ */
216
+ readonly _kind: "object";
217
+ /**
218
+ * The shape of the object, mapping property names to their types.
219
+ */
220
+ readonly shape: Record<string, TypeFactoryType>;
221
+ }
222
+
223
+ /**
224
+ * Represents a record type (index signature) in the type factory system.
225
+ * @alpha
226
+ */
227
+ export interface TypeFactoryRecord extends TypeFactoryType {
228
+ /**
229
+ * {@inheritDoc TypeFactoryType._kind}
230
+ */
231
+ readonly _kind: "record";
232
+ /**
233
+ * The type of the record's keys.
234
+ */
235
+ readonly keyType: TypeFactoryType;
236
+ /**
237
+ * The type of the record's values.
238
+ */
239
+ readonly valueType: TypeFactoryType;
240
+ }
241
+
242
+ /**
243
+ * Represents a Map type in the type factory system.
244
+ * @alpha
245
+ */
246
+ export interface TypeFactoryMap extends TypeFactoryType {
247
+ /**
248
+ * {@inheritDoc TypeFactoryType._kind}
249
+ */
250
+ readonly _kind: "map";
251
+ /**
252
+ * The type of the map's keys.
253
+ */
254
+ readonly keyType: TypeFactoryType;
255
+ /**
256
+ * The type of the map's values.
257
+ */
258
+ readonly valueType: TypeFactoryType;
259
+ }
260
+
261
+ /**
262
+ * Represents a tuple type with fixed-length items and optional rest elements in the type factory system.
263
+ * @alpha
264
+ */
265
+ export interface TypeFactoryTuple extends TypeFactoryType {
266
+ /**
267
+ * {@inheritDoc TypeFactoryType._kind}
268
+ */
269
+ readonly _kind: "tuple";
270
+ /**
271
+ * The fixed-length items in the tuple.
272
+ */
273
+ readonly items: readonly TypeFactoryType[];
274
+ /**
275
+ * Optional rest element type for variable-length tuples.
276
+ */
277
+ readonly rest?: TypeFactoryType;
278
+ }
279
+
280
+ /**
281
+ * Represents a union type in the type factory system.
282
+ * @alpha
283
+ */
284
+ export interface TypeFactoryUnion extends TypeFactoryType {
285
+ /**
286
+ * {@inheritDoc TypeFactoryType._kind}
287
+ */
288
+ readonly _kind: "union";
289
+ /**
290
+ * The possible types in the union.
291
+ */
292
+ readonly options: readonly TypeFactoryType[];
293
+ }
294
+
295
+ /**
296
+ * Represents an intersection type in the type factory system.
297
+ * @alpha
298
+ */
299
+ export interface TypeFactoryIntersection extends TypeFactoryType {
300
+ /**
301
+ * {@inheritDoc TypeFactoryType._kind}
302
+ */
303
+ readonly _kind: "intersection";
304
+ /**
305
+ * The types to intersect.
306
+ */
307
+ readonly types: readonly TypeFactoryType[];
308
+ }
309
+
310
+ /**
311
+ * Represents a literal type (specific string, number, or boolean value) in the type factory system.
312
+ * @alpha
313
+ */
314
+ export interface TypeFactoryLiteral extends TypeFactoryType {
315
+ /**
316
+ * {@inheritDoc TypeFactoryType._kind}
317
+ */
318
+ readonly _kind: "literal";
319
+ /**
320
+ * The specific literal value.
321
+ */
322
+ readonly value: string | number | boolean;
323
+ }
324
+
325
+ /**
326
+ * Represents an optional type modifier in the type factory system.
327
+ * @alpha
328
+ */
329
+ export interface TypeFactoryOptional extends TypeFactoryType {
330
+ /**
331
+ * {@inheritDoc TypeFactoryType._kind}
332
+ */
333
+ readonly _kind: "optional";
334
+ /**
335
+ * The inner type that is optional.
336
+ */
337
+ readonly innerType: TypeFactoryType;
338
+ }
339
+
340
+ /**
341
+ * Represents a readonly type modifier in the type factory system.
342
+ * @alpha
343
+ */
344
+ export interface TypeFactoryReadonly extends TypeFactoryType {
345
+ /**
346
+ * {@inheritDoc TypeFactoryType._kind}
347
+ */
348
+ readonly _kind: "readonly";
349
+ /**
350
+ * The inner type that is readonly.
351
+ */
352
+ readonly innerType: TypeFactoryType;
353
+ }
354
+
355
+ /**
356
+ * Represents a function parameter as a tuple of [name, type].
357
+ * @alpha
358
+ */
359
+ export type TypeFactoryFunctionParameter = readonly [name: string, type: TypeFactoryType];
360
+
361
+ /**
362
+ * Represents a function type in the type factory system.
363
+ * @alpha
364
+ */
365
+ export interface TypeFactoryFunction extends TypeFactoryType {
366
+ /**
367
+ * {@inheritDoc TypeFactoryType._kind}
368
+ */
369
+ readonly _kind: "function";
370
+ /**
371
+ * The function parameters.
372
+ */
373
+ readonly parameters: readonly TypeFactoryFunctionParameter[];
374
+ /**
375
+ * The function return type.
376
+ */
377
+ readonly returnType: TypeFactoryType;
378
+ /**
379
+ * Optional rest parameter for variable-length argument lists.
380
+ */
381
+ readonly restParameter?: TypeFactoryFunctionParameter;
382
+ }
383
+
384
+ /**
385
+ * Represents an instanceof type that references a class in the type factory system.
386
+ * @alpha
387
+ */
388
+ export interface TypeFactoryInstanceOf extends TypeFactoryType {
389
+ /**
390
+ * The kind of type this represents.
391
+ */
392
+ readonly _kind: "instanceof";
393
+ /**
394
+ * The constructor to reference.
395
+ */
396
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
397
+ readonly constructor: new (
398
+ ...args: any[]
399
+ ) => unknown;
400
+ }
401
+
402
+ /**
403
+ * Namespace containing type factory functions for building type definitions
404
+ * that describe method signatures and property types to an LLM agent.
405
+ *
406
+ * @internal
407
+ */
408
+ export const typeFactory = {
409
+ /**
410
+ * Create a string type.
411
+ * @alpha
412
+ */
413
+ string(): TypeFactoryString {
414
+ return { _kind: "string" };
415
+ },
416
+
417
+ /**
418
+ * Create a number type.
419
+ * @alpha
420
+ */
421
+ number(): TypeFactoryNumber {
422
+ return { _kind: "number" };
423
+ },
424
+
425
+ /**
426
+ * Create a boolean type.
427
+ * @alpha
428
+ */
429
+ boolean(): TypeFactoryBoolean {
430
+ return { _kind: "boolean" };
431
+ },
432
+
433
+ /**
434
+ * Create a Date type.
435
+ * @alpha
436
+ */
437
+ date(): TypeFactoryDate {
438
+ return { _kind: "date" };
439
+ },
440
+
441
+ /**
442
+ * Create a void type.
443
+ * @alpha
444
+ */
445
+ void(): TypeFactoryVoid {
446
+ return { _kind: "void" };
447
+ },
448
+
449
+ /**
450
+ * Create an undefined type.
451
+ * @alpha
452
+ */
453
+ undefined(): TypeFactoryUndefined {
454
+ return { _kind: "undefined" };
455
+ },
456
+
457
+ /**
458
+ * Create a null type.
459
+ * @alpha
460
+ */
461
+ null(): TypeFactoryNull {
462
+ return { _kind: "null" };
463
+ },
464
+
465
+ /**
466
+ * Create an unknown type.
467
+ * @alpha
468
+ */
469
+ unknown(): TypeFactoryUnknown {
470
+ return { _kind: "unknown" };
471
+ },
472
+
473
+ /**
474
+ * Create an array type.
475
+ * @alpha
476
+ */
477
+ array(element: TypeFactoryType): TypeFactoryArray {
478
+ return { _kind: "array", element };
479
+ },
480
+
481
+ /**
482
+ * Create a Promise type.
483
+ * @alpha
484
+ */
485
+ promise(innerType: TypeFactoryType): TypeFactoryPromise {
486
+ return { _kind: "promise", innerType };
487
+ },
488
+
489
+ /**
490
+ * Create an object type.
491
+ * @alpha
492
+ */
493
+ object(shape: Record<string, TypeFactoryType>): TypeFactoryObject {
494
+ return { _kind: "object", shape };
495
+ },
496
+
497
+ /**
498
+ * Create a record type.
499
+ * @alpha
500
+ */
501
+ record(keyType: TypeFactoryType, valueType: TypeFactoryType): TypeFactoryRecord {
502
+ return { _kind: "record", keyType, valueType };
503
+ },
504
+
505
+ /**
506
+ * Create a map type.
507
+ * @alpha
508
+ */
509
+ map(keyType: TypeFactoryType, valueType: TypeFactoryType): TypeFactoryMap {
510
+ return { _kind: "map", keyType, valueType };
511
+ },
512
+
513
+ /**
514
+ * Create a tuple type.
515
+ * @alpha
516
+ */
517
+ tuple(items: readonly TypeFactoryType[], rest?: TypeFactoryType): TypeFactoryTuple {
518
+ if (items.length === 0 && rest === undefined) {
519
+ throw new UsageError(
520
+ "typeFactory.tuple requires at least one item or a rest type. Empty tuples are not supported.",
521
+ );
522
+ }
523
+ return rest === undefined ? { _kind: "tuple", items } : { _kind: "tuple", items, rest };
524
+ },
525
+
526
+ /**
527
+ * Create a union type.
528
+ * @alpha
529
+ */
530
+ union(options: readonly TypeFactoryType[]): TypeFactoryUnion {
531
+ if (options.length === 0) {
532
+ throw new UsageError(
533
+ "typeFactory.union requires at least one option. Empty unions are not valid TypeScript types.",
534
+ );
535
+ }
536
+ return { _kind: "union", options };
537
+ },
538
+
539
+ /**
540
+ * Create an intersection type.
541
+ * @alpha
542
+ */
543
+ intersection(types: readonly TypeFactoryType[]): TypeFactoryIntersection {
544
+ if (types.length === 0) {
545
+ throw new UsageError(
546
+ "typeFactory.intersection requires at least one type. Empty intersections are not valid TypeScript types.",
547
+ );
548
+ }
549
+ return { _kind: "intersection", types };
550
+ },
551
+
552
+ /**
553
+ * Create a literal type.
554
+ * @alpha
555
+ */
556
+ literal(value: string | number | boolean): TypeFactoryLiteral {
557
+ return { _kind: "literal", value };
558
+ },
559
+
560
+ /**
561
+ * Create an optional type.
562
+ * @alpha
563
+ */
564
+ optional(innerType: TypeFactoryType): TypeFactoryOptional {
565
+ return { _kind: "optional", innerType };
566
+ },
567
+
568
+ /**
569
+ * Create a readonly type.
570
+ * @alpha
571
+ */
572
+ readonly(innerType: TypeFactoryType): TypeFactoryReadonly {
573
+ return { _kind: "readonly", innerType };
574
+ },
575
+
576
+ /**
577
+ * Create a function type.
578
+ * @alpha
579
+ */
580
+ function(
581
+ parameters: readonly TypeFactoryFunctionParameter[],
582
+ returnType: TypeFactoryType,
583
+ restParameter?: TypeFactoryFunctionParameter,
584
+ ): TypeFactoryFunction {
585
+ return restParameter === undefined
586
+ ? { _kind: "function", parameters, returnType }
587
+ : { _kind: "function", parameters, returnType, restParameter };
588
+ },
589
+
590
+ /**
591
+ * Create an instanceOf type for a class.
592
+ * @remarks
593
+ * This is an untyped version. `@fluidframework/tree-agent` re-exports a
594
+ * narrower override that constrains the constructor to `ObjectNodeSchema`.
595
+ * @internal
596
+ */
597
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
598
+ instanceOf(constructor: new (...args: any[]) => unknown): TypeFactoryInstanceOf {
599
+ return { _kind: "instanceof", constructor };
600
+ },
601
+ };
@@ -0,0 +1,7 @@
1
+ {
2
+ // This config must be used in a "type": "commonjs" environment. (Use `fluid-tsc commonjs`.)
3
+ "extends": "./tsconfig.json",
4
+ "compilerOptions": {
5
+ "outDir": "./dist",
6
+ },
7
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "extends": "../../../common/build/build-common/tsconfig.node16.json",
3
+ "include": ["src/**/*"],
4
+ "exclude": ["src/test/**/*"],
5
+ "compilerOptions": {
6
+ "rootDir": "./src",
7
+ "outDir": "./lib",
8
+ "noImplicitAny": true,
9
+ "preserveConstEnums": true,
10
+ "exactOptionalPropertyTypes": false,
11
+ "lib": ["ES2020", "DOM", "DOM.Iterable"],
12
+ "skipLibCheck": true,
13
+ },
14
+ }
package/tsdoc.json ADDED
@@ -0,0 +1,4 @@
1
+ {
2
+ "$schema": "https://developer.microsoft.com/json-schemas/tsdoc/v0/tsdoc.schema.json",
3
+ "extends": ["../../../common/build/build-common/tsdoc-base.json"]
4
+ }