@fluidframework/tree 2.4.0-299707 → 2.5.0-302463

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 (289) hide show
  1. package/.vscode/settings.json +11 -1
  2. package/CHANGELOG.md +192 -0
  3. package/api-report/tree.alpha.api.md +150 -52
  4. package/api-report/tree.beta.api.md +85 -39
  5. package/api-report/tree.legacy.alpha.api.md +83 -38
  6. package/api-report/tree.legacy.public.api.md +83 -38
  7. package/api-report/tree.public.api.md +83 -38
  8. package/dist/alpha.d.ts +17 -2
  9. package/dist/beta.d.ts +6 -0
  10. package/dist/events/interop.d.ts +1 -7
  11. package/dist/events/interop.d.ts.map +1 -1
  12. package/dist/events/interop.js.map +1 -1
  13. package/dist/feature-libraries/chunked-forest/basicChunk.js +1 -1
  14. package/dist/feature-libraries/chunked-forest/basicChunk.js.map +1 -1
  15. package/dist/feature-libraries/chunked-forest/uniformChunk.js +1 -1
  16. package/dist/feature-libraries/chunked-forest/uniformChunk.js.map +1 -1
  17. package/dist/feature-libraries/flex-tree/lazyField.js +1 -1
  18. package/dist/feature-libraries/flex-tree/lazyField.js.map +1 -1
  19. package/dist/feature-libraries/modular-schema/modularChangeFamily.d.ts.map +1 -1
  20. package/dist/feature-libraries/modular-schema/modularChangeFamily.js +3 -3
  21. package/dist/feature-libraries/modular-schema/modularChangeFamily.js.map +1 -1
  22. package/dist/feature-libraries/object-forest/objectForest.js +1 -1
  23. package/dist/feature-libraries/object-forest/objectForest.js.map +1 -1
  24. package/dist/feature-libraries/treeCursorUtils.js +2 -2
  25. package/dist/feature-libraries/treeCursorUtils.js.map +1 -1
  26. package/dist/index.d.ts +2 -2
  27. package/dist/index.d.ts.map +1 -1
  28. package/dist/index.js +1 -2
  29. package/dist/index.js.map +1 -1
  30. package/dist/internalTypes.d.ts +1 -1
  31. package/dist/internalTypes.d.ts.map +1 -1
  32. package/dist/internalTypes.js.map +1 -1
  33. package/dist/legacy.d.ts +6 -0
  34. package/dist/packageVersion.d.ts +1 -1
  35. package/dist/packageVersion.js +1 -1
  36. package/dist/packageVersion.js.map +1 -1
  37. package/dist/public.d.ts +6 -0
  38. package/dist/shared-tree/schematizingTreeView.d.ts.map +1 -1
  39. package/dist/shared-tree/schematizingTreeView.js +1 -1
  40. package/dist/shared-tree/schematizingTreeView.js.map +1 -1
  41. package/dist/shared-tree/sharedTree.d.ts +1 -1
  42. package/dist/shared-tree/sharedTree.d.ts.map +1 -1
  43. package/dist/shared-tree/sharedTree.js +1 -1
  44. package/dist/shared-tree/sharedTree.js.map +1 -1
  45. package/dist/shared-tree/treeApi.d.ts +6 -6
  46. package/dist/shared-tree/treeApi.d.ts.map +1 -1
  47. package/dist/shared-tree/treeApi.js.map +1 -1
  48. package/dist/shared-tree/treeCheckout.d.ts.map +1 -1
  49. package/dist/shared-tree/treeCheckout.js +2 -0
  50. package/dist/shared-tree/treeCheckout.js.map +1 -1
  51. package/dist/shared-tree-core/branch.js +1 -1
  52. package/dist/shared-tree-core/branch.js.map +1 -1
  53. package/dist/shared-tree-core/editManager.d.ts.map +1 -1
  54. package/dist/shared-tree-core/editManager.js +12 -7
  55. package/dist/shared-tree-core/editManager.js.map +1 -1
  56. package/dist/simple-tree/api/create.d.ts +3 -3
  57. package/dist/simple-tree/api/create.d.ts.map +1 -1
  58. package/dist/simple-tree/api/create.js.map +1 -1
  59. package/dist/simple-tree/api/customTree.js +7 -7
  60. package/dist/simple-tree/api/customTree.js.map +1 -1
  61. package/dist/simple-tree/api/index.d.ts +3 -3
  62. package/dist/simple-tree/api/index.d.ts.map +1 -1
  63. package/dist/simple-tree/api/index.js +1 -2
  64. package/dist/simple-tree/api/index.js.map +1 -1
  65. package/dist/simple-tree/api/schemaCreationUtilities.d.ts +33 -25
  66. package/dist/simple-tree/api/schemaCreationUtilities.d.ts.map +1 -1
  67. package/dist/simple-tree/api/schemaCreationUtilities.js +31 -21
  68. package/dist/simple-tree/api/schemaCreationUtilities.js.map +1 -1
  69. package/dist/simple-tree/api/schemaFactory.d.ts +5 -1
  70. package/dist/simple-tree/api/schemaFactory.d.ts.map +1 -1
  71. package/dist/simple-tree/api/schemaFactory.js +4 -0
  72. package/dist/simple-tree/api/schemaFactory.js.map +1 -1
  73. package/dist/simple-tree/api/simpleSchemaToJsonSchema.js +3 -3
  74. package/dist/simple-tree/api/simpleSchemaToJsonSchema.js.map +1 -1
  75. package/dist/simple-tree/api/tree.d.ts +13 -4
  76. package/dist/simple-tree/api/tree.d.ts.map +1 -1
  77. package/dist/simple-tree/api/tree.js.map +1 -1
  78. package/dist/simple-tree/api/treeApiBeta.d.ts +14 -2
  79. package/dist/simple-tree/api/treeApiBeta.d.ts.map +1 -1
  80. package/dist/simple-tree/api/treeApiBeta.js +23 -8
  81. package/dist/simple-tree/api/treeApiBeta.js.map +1 -1
  82. package/dist/simple-tree/api/typesUnsafe.d.ts +69 -17
  83. package/dist/simple-tree/api/typesUnsafe.d.ts.map +1 -1
  84. package/dist/simple-tree/api/typesUnsafe.js.map +1 -1
  85. package/dist/simple-tree/arrayNode.d.ts +14 -3
  86. package/dist/simple-tree/arrayNode.d.ts.map +1 -1
  87. package/dist/simple-tree/arrayNode.js.map +1 -1
  88. package/dist/simple-tree/core/schemaCaching.js +1 -1
  89. package/dist/simple-tree/core/schemaCaching.js.map +1 -1
  90. package/dist/simple-tree/core/treeNodeKernel.d.ts +6 -1
  91. package/dist/simple-tree/core/treeNodeKernel.d.ts.map +1 -1
  92. package/dist/simple-tree/core/treeNodeKernel.js +7 -1
  93. package/dist/simple-tree/core/treeNodeKernel.js.map +1 -1
  94. package/dist/simple-tree/core/treeNodeSchema.d.ts +10 -6
  95. package/dist/simple-tree/core/treeNodeSchema.d.ts.map +1 -1
  96. package/dist/simple-tree/core/treeNodeSchema.js.map +1 -1
  97. package/dist/simple-tree/core/unhydratedFlexTree.js +1 -1
  98. package/dist/simple-tree/core/unhydratedFlexTree.js.map +1 -1
  99. package/dist/simple-tree/core/withType.d.ts +3 -1
  100. package/dist/simple-tree/core/withType.d.ts.map +1 -1
  101. package/dist/simple-tree/core/withType.js.map +1 -1
  102. package/dist/simple-tree/index.d.ts +4 -4
  103. package/dist/simple-tree/index.d.ts.map +1 -1
  104. package/dist/simple-tree/index.js +1 -2
  105. package/dist/simple-tree/index.js.map +1 -1
  106. package/dist/simple-tree/mapNode.d.ts.map +1 -1
  107. package/dist/simple-tree/mapNode.js +3 -0
  108. package/dist/simple-tree/mapNode.js.map +1 -1
  109. package/dist/simple-tree/objectNode.d.ts +4 -2
  110. package/dist/simple-tree/objectNode.d.ts.map +1 -1
  111. package/dist/simple-tree/objectNode.js.map +1 -1
  112. package/dist/simple-tree/schemaTypes.d.ts +168 -14
  113. package/dist/simple-tree/schemaTypes.d.ts.map +1 -1
  114. package/dist/simple-tree/schemaTypes.js +6 -1
  115. package/dist/simple-tree/schemaTypes.js.map +1 -1
  116. package/dist/simple-tree/toFlexSchema.js +2 -2
  117. package/dist/simple-tree/toFlexSchema.js.map +1 -1
  118. package/dist/simple-tree/toMapTree.d.ts +4 -2
  119. package/dist/simple-tree/toMapTree.d.ts.map +1 -1
  120. package/dist/simple-tree/toMapTree.js.map +1 -1
  121. package/dist/util/index.d.ts +2 -1
  122. package/dist/util/index.d.ts.map +1 -1
  123. package/dist/util/index.js +3 -1
  124. package/dist/util/index.js.map +1 -1
  125. package/dist/util/typeUtils.d.ts +61 -0
  126. package/dist/util/typeUtils.d.ts.map +1 -1
  127. package/dist/util/typeUtils.js +27 -0
  128. package/dist/util/typeUtils.js.map +1 -1
  129. package/lib/alpha.d.ts +17 -2
  130. package/lib/beta.d.ts +6 -0
  131. package/lib/events/interop.d.ts +1 -7
  132. package/lib/events/interop.d.ts.map +1 -1
  133. package/lib/events/interop.js.map +1 -1
  134. package/lib/feature-libraries/chunked-forest/basicChunk.js +1 -1
  135. package/lib/feature-libraries/chunked-forest/basicChunk.js.map +1 -1
  136. package/lib/feature-libraries/chunked-forest/uniformChunk.js +1 -1
  137. package/lib/feature-libraries/chunked-forest/uniformChunk.js.map +1 -1
  138. package/lib/feature-libraries/flex-tree/lazyField.js +1 -1
  139. package/lib/feature-libraries/flex-tree/lazyField.js.map +1 -1
  140. package/lib/feature-libraries/modular-schema/modularChangeFamily.d.ts.map +1 -1
  141. package/lib/feature-libraries/modular-schema/modularChangeFamily.js +3 -3
  142. package/lib/feature-libraries/modular-schema/modularChangeFamily.js.map +1 -1
  143. package/lib/feature-libraries/object-forest/objectForest.js +1 -1
  144. package/lib/feature-libraries/object-forest/objectForest.js.map +1 -1
  145. package/lib/feature-libraries/treeCursorUtils.js +2 -2
  146. package/lib/feature-libraries/treeCursorUtils.js.map +1 -1
  147. package/lib/index.d.ts +2 -2
  148. package/lib/index.d.ts.map +1 -1
  149. package/lib/index.js +1 -1
  150. package/lib/index.js.map +1 -1
  151. package/lib/internalTypes.d.ts +1 -1
  152. package/lib/internalTypes.d.ts.map +1 -1
  153. package/lib/internalTypes.js.map +1 -1
  154. package/lib/legacy.d.ts +6 -0
  155. package/lib/packageVersion.d.ts +1 -1
  156. package/lib/packageVersion.js +1 -1
  157. package/lib/packageVersion.js.map +1 -1
  158. package/lib/public.d.ts +6 -0
  159. package/lib/shared-tree/schematizingTreeView.d.ts.map +1 -1
  160. package/lib/shared-tree/schematizingTreeView.js +1 -1
  161. package/lib/shared-tree/schematizingTreeView.js.map +1 -1
  162. package/lib/shared-tree/sharedTree.d.ts +1 -1
  163. package/lib/shared-tree/sharedTree.d.ts.map +1 -1
  164. package/lib/shared-tree/sharedTree.js +1 -1
  165. package/lib/shared-tree/sharedTree.js.map +1 -1
  166. package/lib/shared-tree/treeApi.d.ts +6 -6
  167. package/lib/shared-tree/treeApi.d.ts.map +1 -1
  168. package/lib/shared-tree/treeApi.js.map +1 -1
  169. package/lib/shared-tree/treeCheckout.d.ts.map +1 -1
  170. package/lib/shared-tree/treeCheckout.js +2 -0
  171. package/lib/shared-tree/treeCheckout.js.map +1 -1
  172. package/lib/shared-tree-core/branch.js +1 -1
  173. package/lib/shared-tree-core/branch.js.map +1 -1
  174. package/lib/shared-tree-core/editManager.d.ts.map +1 -1
  175. package/lib/shared-tree-core/editManager.js +12 -7
  176. package/lib/shared-tree-core/editManager.js.map +1 -1
  177. package/lib/simple-tree/api/create.d.ts +3 -3
  178. package/lib/simple-tree/api/create.d.ts.map +1 -1
  179. package/lib/simple-tree/api/create.js.map +1 -1
  180. package/lib/simple-tree/api/customTree.js +7 -7
  181. package/lib/simple-tree/api/customTree.js.map +1 -1
  182. package/lib/simple-tree/api/index.d.ts +3 -3
  183. package/lib/simple-tree/api/index.d.ts.map +1 -1
  184. package/lib/simple-tree/api/index.js +1 -1
  185. package/lib/simple-tree/api/index.js.map +1 -1
  186. package/lib/simple-tree/api/schemaCreationUtilities.d.ts +33 -25
  187. package/lib/simple-tree/api/schemaCreationUtilities.d.ts.map +1 -1
  188. package/lib/simple-tree/api/schemaCreationUtilities.js +30 -19
  189. package/lib/simple-tree/api/schemaCreationUtilities.js.map +1 -1
  190. package/lib/simple-tree/api/schemaFactory.d.ts +5 -1
  191. package/lib/simple-tree/api/schemaFactory.d.ts.map +1 -1
  192. package/lib/simple-tree/api/schemaFactory.js +4 -0
  193. package/lib/simple-tree/api/schemaFactory.js.map +1 -1
  194. package/lib/simple-tree/api/simpleSchemaToJsonSchema.js +3 -3
  195. package/lib/simple-tree/api/simpleSchemaToJsonSchema.js.map +1 -1
  196. package/lib/simple-tree/api/tree.d.ts +13 -4
  197. package/lib/simple-tree/api/tree.d.ts.map +1 -1
  198. package/lib/simple-tree/api/tree.js.map +1 -1
  199. package/lib/simple-tree/api/treeApiBeta.d.ts +14 -2
  200. package/lib/simple-tree/api/treeApiBeta.d.ts.map +1 -1
  201. package/lib/simple-tree/api/treeApiBeta.js +23 -8
  202. package/lib/simple-tree/api/treeApiBeta.js.map +1 -1
  203. package/lib/simple-tree/api/typesUnsafe.d.ts +69 -17
  204. package/lib/simple-tree/api/typesUnsafe.d.ts.map +1 -1
  205. package/lib/simple-tree/api/typesUnsafe.js.map +1 -1
  206. package/lib/simple-tree/arrayNode.d.ts +14 -3
  207. package/lib/simple-tree/arrayNode.d.ts.map +1 -1
  208. package/lib/simple-tree/arrayNode.js.map +1 -1
  209. package/lib/simple-tree/core/schemaCaching.js +1 -1
  210. package/lib/simple-tree/core/schemaCaching.js.map +1 -1
  211. package/lib/simple-tree/core/treeNodeKernel.d.ts +6 -1
  212. package/lib/simple-tree/core/treeNodeKernel.d.ts.map +1 -1
  213. package/lib/simple-tree/core/treeNodeKernel.js +7 -1
  214. package/lib/simple-tree/core/treeNodeKernel.js.map +1 -1
  215. package/lib/simple-tree/core/treeNodeSchema.d.ts +10 -6
  216. package/lib/simple-tree/core/treeNodeSchema.d.ts.map +1 -1
  217. package/lib/simple-tree/core/treeNodeSchema.js.map +1 -1
  218. package/lib/simple-tree/core/unhydratedFlexTree.js +1 -1
  219. package/lib/simple-tree/core/unhydratedFlexTree.js.map +1 -1
  220. package/lib/simple-tree/core/withType.d.ts +3 -1
  221. package/lib/simple-tree/core/withType.d.ts.map +1 -1
  222. package/lib/simple-tree/core/withType.js.map +1 -1
  223. package/lib/simple-tree/index.d.ts +4 -4
  224. package/lib/simple-tree/index.d.ts.map +1 -1
  225. package/lib/simple-tree/index.js +2 -2
  226. package/lib/simple-tree/index.js.map +1 -1
  227. package/lib/simple-tree/mapNode.d.ts.map +1 -1
  228. package/lib/simple-tree/mapNode.js +3 -0
  229. package/lib/simple-tree/mapNode.js.map +1 -1
  230. package/lib/simple-tree/objectNode.d.ts +4 -2
  231. package/lib/simple-tree/objectNode.d.ts.map +1 -1
  232. package/lib/simple-tree/objectNode.js.map +1 -1
  233. package/lib/simple-tree/schemaTypes.d.ts +168 -14
  234. package/lib/simple-tree/schemaTypes.d.ts.map +1 -1
  235. package/lib/simple-tree/schemaTypes.js +6 -1
  236. package/lib/simple-tree/schemaTypes.js.map +1 -1
  237. package/lib/simple-tree/toFlexSchema.js +2 -2
  238. package/lib/simple-tree/toFlexSchema.js.map +1 -1
  239. package/lib/simple-tree/toMapTree.d.ts +4 -2
  240. package/lib/simple-tree/toMapTree.d.ts.map +1 -1
  241. package/lib/simple-tree/toMapTree.js.map +1 -1
  242. package/lib/util/index.d.ts +2 -1
  243. package/lib/util/index.d.ts.map +1 -1
  244. package/lib/util/index.js +1 -0
  245. package/lib/util/index.js.map +1 -1
  246. package/lib/util/typeUtils.d.ts +61 -0
  247. package/lib/util/typeUtils.d.ts.map +1 -1
  248. package/lib/util/typeUtils.js +25 -1
  249. package/lib/util/typeUtils.js.map +1 -1
  250. package/package.json +48 -32
  251. package/src/events/interop.ts +1 -12
  252. package/src/feature-libraries/chunked-forest/basicChunk.ts +1 -1
  253. package/src/feature-libraries/chunked-forest/uniformChunk.ts +1 -1
  254. package/src/feature-libraries/flex-tree/lazyField.ts +1 -1
  255. package/src/feature-libraries/modular-schema/modularChangeFamily.ts +9 -3
  256. package/src/feature-libraries/object-forest/objectForest.ts +1 -1
  257. package/src/feature-libraries/treeCursorUtils.ts +2 -2
  258. package/src/index.ts +16 -1
  259. package/src/internalTypes.ts +4 -0
  260. package/src/packageVersion.ts +1 -1
  261. package/src/shared-tree/schematizingTreeView.ts +7 -3
  262. package/src/shared-tree/sharedTree.ts +5 -3
  263. package/src/shared-tree/treeApi.ts +19 -9
  264. package/src/shared-tree/treeCheckout.ts +2 -0
  265. package/src/shared-tree-core/branch.ts +1 -1
  266. package/src/shared-tree-core/editManager.ts +16 -7
  267. package/src/simple-tree/api/create.ts +12 -7
  268. package/src/simple-tree/api/customTree.ts +7 -7
  269. package/src/simple-tree/api/index.ts +6 -1
  270. package/src/simple-tree/api/schemaCreationUtilities.ts +58 -35
  271. package/src/simple-tree/api/schemaFactory.ts +4 -0
  272. package/src/simple-tree/api/simpleSchemaToJsonSchema.ts +3 -3
  273. package/src/simple-tree/api/tree.ts +27 -4
  274. package/src/simple-tree/api/treeApiBeta.ts +69 -3
  275. package/src/simple-tree/api/typesUnsafe.ts +126 -30
  276. package/src/simple-tree/arrayNode.ts +34 -14
  277. package/src/simple-tree/core/schemaCaching.ts +1 -1
  278. package/src/simple-tree/core/treeNodeKernel.ts +10 -2
  279. package/src/simple-tree/core/treeNodeSchema.ts +11 -6
  280. package/src/simple-tree/core/unhydratedFlexTree.ts +1 -1
  281. package/src/simple-tree/core/withType.ts +10 -1
  282. package/src/simple-tree/index.ts +19 -2
  283. package/src/simple-tree/mapNode.ts +6 -3
  284. package/src/simple-tree/objectNode.ts +5 -3
  285. package/src/simple-tree/schemaTypes.ts +215 -23
  286. package/src/simple-tree/toFlexSchema.ts +2 -2
  287. package/src/simple-tree/toMapTree.ts +4 -1
  288. package/src/util/index.ts +6 -0
  289. package/src/util/typeUtils.ts +87 -0
@@ -8,12 +8,18 @@ import { Lazy } from "@fluidframework/core-utils/internal";
8
8
  import { UsageError } from "@fluidframework/telemetry-utils/internal";
9
9
 
10
10
  import type { NodeKeyManager } from "../feature-libraries/index.js";
11
- import { type MakeNominal, brand, isReadonlyArray } from "../util/index.js";
11
+ import {
12
+ type MakeNominal,
13
+ brand,
14
+ isReadonlyArray,
15
+ type UnionToIntersection,
16
+ } from "../util/index.js";
12
17
  import type {
13
18
  Unhydrated,
14
19
  NodeKind,
15
20
  TreeNodeSchema,
16
21
  TreeNodeSchemaClass,
22
+ TreeNode,
17
23
  } from "./core/index.js";
18
24
  import type { FieldKey } from "../core/index.js";
19
25
  import type { InsertableContent } from "./toMapTree.js";
@@ -25,7 +31,7 @@ import { isLazy, type FlexListToUnion, type LazyItem } from "./flexList.js";
25
31
  export function isTreeNodeSchemaClass<
26
32
  Name extends string,
27
33
  Kind extends NodeKind,
28
- TNode,
34
+ TNode extends TreeNode | TreeLeafValue,
29
35
  TBuild,
30
36
  ImplicitlyConstructable extends boolean,
31
37
  Info,
@@ -39,8 +45,16 @@ export function isTreeNodeSchemaClass<
39
45
  * Types for use in fields.
40
46
  * @remarks
41
47
  * Type constraint used in schema declaration APIs.
48
+ *
49
+ * The order of types in the array is not significant.
50
+ * Additionally, it is legal for users of this type to have the runtime and compile time order of items within this array not match.
51
+ * Therefor to ensure type safety, these arrays should not be indexed, and instead just be iterated.
52
+ *
53
+ * Ideally this restriction would be modeled in the type itself, but it is not ergonomic to do so as there is no easy (when compared to arrays)
54
+ * way to declare and manipulate unordered sets of types in TypeScript.
55
+ *
42
56
  * Not intended for direct use outside of package.
43
- * @public
57
+ * @system @public
44
58
  */
45
59
  export type AllowedTypes = readonly LazyItem<TreeNodeSchema>[];
46
60
 
@@ -381,39 +395,122 @@ export type ImplicitAllowedTypes = AllowedTypes | TreeNodeSchema;
381
395
  export type ImplicitFieldSchema = FieldSchema | ImplicitAllowedTypes;
382
396
 
383
397
  /**
384
- * Converts ImplicitFieldSchema to the corresponding tree node's field type.
398
+ * Converts an `ImplicitFieldSchema` to a property type suitable for reading a field with this that schema.
399
+ *
400
+ * @typeparam TSchema - When non-exact schema are provided this errors on the side of returning too general of a type (a conservative union of all possibilities).
401
+ * This is ideal for "output APIs" - i.e. it converts the schema type to the runtime type that a user will _read_ from the tree.
402
+ * Examples of such "non-exact" schema include `ImplicitFieldSchema`, `ImplicitAllowedTypes`, and TypeScript unions of schema types.
385
403
  * @public
386
404
  */
387
405
  export type TreeFieldFromImplicitField<TSchema extends ImplicitFieldSchema = FieldSchema> =
388
406
  TSchema extends FieldSchema<infer Kind, infer Types>
389
- ? ApplyKind<TreeNodeFromImplicitAllowedTypes<Types>, Kind, false>
407
+ ? ApplyKind<TreeNodeFromImplicitAllowedTypes<Types>, Kind>
390
408
  : TSchema extends ImplicitAllowedTypes
391
409
  ? TreeNodeFromImplicitAllowedTypes<TSchema>
392
- : unknown;
410
+ : TreeNode | TreeLeafValue | undefined;
393
411
 
394
412
  /**
395
413
  * Type of content that can be inserted into the tree for a field of the given schema.
414
+ *
415
+ * @see {@link Input}
416
+ *
417
+ * @typeparam TSchemaInput - Schema to process.
418
+ * @typeparam TSchema - Do not specify: default value used as implementation detail.
396
419
  * @public
397
420
  */
398
421
  export type InsertableTreeFieldFromImplicitField<
399
- TSchema extends ImplicitFieldSchema = FieldSchema,
400
- > = TSchema extends FieldSchema<infer Kind, infer Types>
401
- ? ApplyKind<InsertableTreeNodeFromImplicitAllowedTypes<Types>, Kind, true>
402
- : TSchema extends ImplicitAllowedTypes
422
+ TSchemaInput extends ImplicitFieldSchema,
423
+ TSchema = UnionToIntersection<TSchemaInput>,
424
+ > = [TSchema] extends [FieldSchema<infer Kind, infer Types>]
425
+ ? ApplyKindInput<InsertableTreeNodeFromImplicitAllowedTypes<Types>, Kind, true>
426
+ : [TSchema] extends [ImplicitAllowedTypes]
403
427
  ? InsertableTreeNodeFromImplicitAllowedTypes<TSchema>
404
- : unknown;
428
+ : never;
429
+
430
+ /**
431
+ * {@inheritdoc (UnsafeUnknownSchema:type)}
432
+ * @alpha
433
+ */
434
+ export const UnsafeUnknownSchema: unique symbol = Symbol("UnsafeUnknownSchema");
435
+
436
+ /**
437
+ * A special type which can be provided to some APIs as the schema type parameter when schema cannot easily be provided at compile time and an unsafe (instead of disabled) editing API is desired.
438
+ * @remarks
439
+ * When used, this means the TypeScript typing should err on the side of completeness (allow all inputs that could be valid).
440
+ * This introduces the risk that out-of-schema data could be allowed at compile time, and only error at runtime.
441
+ *
442
+ * @privateRemarks
443
+ * This only applies to APIs which input data which is expected to be in schema, since APIs outputting have easy mechanisms to do so in a type safe way even when the schema is unknown.
444
+ * In most cases that amounts to returning `TreeNode | TreeLeafValue`.
445
+ *
446
+ * This can be contrasted with the default behavior of TypeScript, which is to require the intersection of the possible types for input APIs,
447
+ * which for unknown schema defining input trees results in the `never` type.
448
+ *
449
+ * Any APIs which use this must produce UsageErrors when out of schema data is encountered, and never produce unrecoverable errors,
450
+ * or silently accept invalid data.
451
+ * This is currently only type exported from the package: the symbol is just used as a way to get a named type.
452
+ * @alpha
453
+ */
454
+ export type UnsafeUnknownSchema = typeof UnsafeUnknownSchema;
455
+
456
+ /**
457
+ * Content which could be inserted into a tree.
458
+ *
459
+ * @see {@link Input}
460
+ * @remarks
461
+ * Extended version of {@link InsertableTreeNodeFromImplicitAllowedTypes} that also allows {@link (UnsafeUnknownSchema:type)}.
462
+ * @alpha
463
+ */
464
+ export type Insertable<TSchema extends ImplicitAllowedTypes | UnsafeUnknownSchema> =
465
+ TSchema extends ImplicitAllowedTypes
466
+ ? InsertableTreeNodeFromImplicitAllowedTypes<TSchema>
467
+ : InsertableContent;
468
+
469
+ /**
470
+ * Content which could be inserted into a field within a tree.
471
+ *
472
+ * @see {@link Input}
473
+ * @remarks
474
+ * Extended version of {@link InsertableTreeFieldFromImplicitField} that also allows {@link (UnsafeUnknownSchema:type)}.
475
+ * @alpha
476
+ */
477
+ export type InsertableField<TSchema extends ImplicitFieldSchema | UnsafeUnknownSchema> = [
478
+ TSchema,
479
+ ] extends [ImplicitFieldSchema]
480
+ ? InsertableTreeFieldFromImplicitField<TSchema>
481
+ : [TSchema] extends [UnsafeUnknownSchema]
482
+ ? InsertableContent | undefined
483
+ : never;
405
484
 
406
485
  /**
407
486
  * Suitable for output.
408
487
  * For input must error on side of excluding undefined instead.
409
488
  * @system @public
410
489
  */
411
- export type ApplyKind<T, Kind extends FieldKind, DefaultsAreOptional extends boolean> = {
490
+ export type ApplyKind<T, Kind extends FieldKind> = {
412
491
  [FieldKind.Required]: T;
413
492
  [FieldKind.Optional]: T | undefined;
414
- [FieldKind.Identifier]: DefaultsAreOptional extends true ? T | undefined : T;
493
+ [FieldKind.Identifier]: T;
415
494
  }[Kind];
416
495
 
496
+ /**
497
+ * Suitable for input.
498
+ *
499
+ * @see {@link Input}
500
+ * @system @public
501
+ */
502
+ export type ApplyKindInput<T, Kind extends FieldKind, DefaultsAreOptional extends boolean> = [
503
+ Kind,
504
+ ] extends [FieldKind.Required]
505
+ ? T
506
+ : [Kind] extends [FieldKind.Optional]
507
+ ? T | undefined
508
+ : [Kind] extends [FieldKind.Identifier]
509
+ ? DefaultsAreOptional extends true
510
+ ? T | undefined
511
+ : T
512
+ : never;
513
+
417
514
  /**
418
515
  * Type of tree node for a field of the given schema.
419
516
  * @public
@@ -426,16 +523,91 @@ export type TreeNodeFromImplicitAllowedTypes<
426
523
  ? NodeFromSchema<FlexListToUnion<TSchema>>
427
524
  : unknown;
428
525
 
526
+ /**
527
+ * This type exists only to be linked from documentation to provide a single linkable place to document some details of
528
+ * "Input" types and how they handle schema.
529
+ *
530
+ * When a schema is used to describe data which is an input into an API, the API is [contravariant](https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)) over the schema.
531
+ * (See also, [TypeScript Variance Annotations](https://www.typescriptlang.org/docs/handbook/2/generics.html#variance-annotations)).
532
+ *
533
+ * Since these schema are expressed using TypeScript types, it is possible for the user of the API to provide non-exact values of these types which has implications that depended on the variance.
534
+ *
535
+ * Consider a field with schema type of `A | B` (where A and B are types of schema).
536
+ *
537
+ * - Reading the field behaves covariantly so {@link NodeFromSchema} of `<A | B>` is the same as `NodeFromSchema<A> | NodeFromSchema<B>`, indicating that either type of node can be read from the field.
538
+ * - Writing to the field behaves contravariantly. Since it is unknown if the node actually has a schema `A` or a schema `B`, the only legal values (known to be in schema regardless of which schema the underlying node has) are values which are legal for both `A & B`.
539
+ *
540
+ * Note that this is distinct from the case where the schema is `[A, B]`.
541
+ * In this case it is known that the field allows both A and B (the field can be set to an A or a B value).
542
+ * When `A | B` is used, the field might allow
543
+ * A but not B (so assigning a B value would be out of schema),
544
+ * B but not A (so assigning an A value would be out of schema)
545
+ * or both A and B.
546
+ *
547
+ * This gets more extreme when given completely unspecified schema.
548
+ * For example if a field is just provided {@link ImplicitFieldSchema}, nothing is known about the content of the field.
549
+ * This means that reading the field (via {@link TreeFieldFromImplicitField}) can give any valid tree field content,
550
+ * but there are no safe values which could be written to the field (since it is unknown what values would be out of schema) so {@link InsertableTreeFieldFromImplicitField} gives `never`.
551
+ *
552
+ * To implement this variance correctly, the computation of types for input and output have to use separate utilities
553
+ * which take very different approaches when encountering non-exact schema like unions or `ImplicitFieldSchema`.
554
+ * The utilities which behave contravariantly (as required to handle input correctly) link this documentation to indicate that this is how they behave.
555
+ *
556
+ * In addition to behaving contravariantly, these input type computation utilities often have further limitations.
557
+ * This is due to TypeScript making it difficult to implement this contravariance exactly.
558
+ * When faced with these implementation limitations these contravariant type computation utilities error on the side of producing overly strict requirements.
559
+ * For example in the above case of `A | B`, the utilities might compute an allowed insertable type as `never` even if there happens to be a common value accepted by both `A` and `B`.
560
+ * Future versions of the API can relax these requirements as the type computations are made more accurate.
561
+ *
562
+ * For a more concrete example: if {@link InsertableTreeFieldFromImplicitField} produced `never` for a schema `A | OptionalField<A>`,
563
+ * a future version could instead return a more flexible but still safe type, like `A`.
564
+ *
565
+ * More generally: try to avoid providing non-exact schema, especially for the fields of other schema.
566
+ * While these APIs attempt to handle such cases correctly, there are limitations and known bugs in this handling.
567
+ * Code using non-exact schema is much more likely to have its compilation break due to updates of this package or even TypeScript,
568
+ * and thus compilation breaks due to edge cases of non-exact schema handling, especially with recursive schema, are not considered breaking changes.
569
+ * This may change as the API become more stable.
570
+ *
571
+ * @privateRemarks
572
+ * There likely is a better way to share this documentation, but none was found at the time of writing.
573
+ *
574
+ * TODO: Once {@link InsertableField} is public, consider using it in the examples above.
575
+ * @system @public
576
+ */
577
+ export type Input<T extends never> = T;
578
+
429
579
  /**
430
580
  * Type of content that can be inserted into the tree for a node of the given schema.
581
+ *
582
+ * @see {@link Input}
583
+ *
584
+ * @typeparam TSchema - Schema to process.
585
+ *
586
+ * @privateRemarks
587
+ * This is a bit overly conservative, since cases like `A | [A]` give never and could give `A`.
431
588
  * @public
432
589
  */
433
- export type InsertableTreeNodeFromImplicitAllowedTypes<
434
- TSchema extends ImplicitAllowedTypes = TreeNodeSchema,
435
- > = TSchema extends TreeNodeSchema
436
- ? InsertableTypedNode<TSchema>
437
- : TSchema extends AllowedTypes
438
- ? InsertableTypedNode<FlexListToUnion<TSchema>>
590
+ export type InsertableTreeNodeFromImplicitAllowedTypes<TSchema extends ImplicitAllowedTypes> =
591
+ [TSchema] extends [TreeNodeSchema]
592
+ ? InsertableTypedNode<TSchema>
593
+ : [TSchema] extends [AllowedTypes]
594
+ ? InsertableTreeNodeFromAllowedTypes<TSchema>
595
+ : never;
596
+
597
+ /**
598
+ * Type of content that can be inserted into the tree for a node of the given schema.
599
+ *
600
+ * @see {@link Input}
601
+ *
602
+ * @typeparam TList - AllowedTypes to process
603
+ * @system @public
604
+ */
605
+ export type InsertableTreeNodeFromAllowedTypes<TList extends AllowedTypes> =
606
+ TList extends readonly [
607
+ LazyItem<infer TSchema extends TreeNodeSchema>,
608
+ ...infer Rest extends AllowedTypes,
609
+ ]
610
+ ? InsertableTypedNode<TSchema> | InsertableTreeNodeFromAllowedTypes<Rest>
439
611
  : never;
440
612
 
441
613
  /**
@@ -453,11 +625,31 @@ export type NodeFromSchema<T extends TreeNodeSchema> = T extends TreeNodeSchema<
453
625
  /**
454
626
  * Data which can be used as a node to be inserted.
455
627
  * Either an unhydrated node, or content to build a new node.
628
+ *
629
+ * @see {@link Input}
630
+ *
631
+ * @typeparam TSchemaInput - Schema to process.
632
+ * @typeparam T - Do not specify: default value used as implementation detail.
633
+ * @privateRemarks
634
+ * This can't really be fully correct, since TreeNodeSchema's TNode is generally use covariantly but this code uses it contravariantly.
635
+ * That makes this TreeNodeSchema actually invariant with respect to TNode, but doing that would break all `extends TreeNodeSchema` clauses.
636
+ * As is, this works correctly in most realistic use-cases.
637
+ *
638
+ * One special case this makes is if the result of NodeFromSchema contains TreeNode, this must be an under constrained schema, so the result is set to never.
639
+ * Note that applying UnionToIntersection on the result of NodeFromSchema<T> does not work since it breaks booleans.
640
+ *
456
641
  * @public
457
642
  */
458
- export type InsertableTypedNode<T extends TreeNodeSchema> =
459
- | (T extends { implicitlyConstructable: true } ? NodeBuilderData<T> : never)
460
- | Unhydrated<NodeFromSchema<T>>;
643
+ export type InsertableTypedNode<
644
+ TSchema extends TreeNodeSchema,
645
+ T = UnionToIntersection<TSchema>,
646
+ > =
647
+ | (T extends TreeNodeSchema<string, NodeKind, TreeNode | TreeLeafValue, never, true>
648
+ ? NodeBuilderData<T>
649
+ : never)
650
+ | (T extends TreeNodeSchema
651
+ ? Unhydrated<TreeNode extends NodeFromSchema<T> ? never : NodeFromSchema<T>>
652
+ : never);
461
653
 
462
654
  /**
463
655
  * Given a node's schema, return the corresponding object from which the node could be built.
@@ -471,7 +663,7 @@ export type InsertableTypedNode<T extends TreeNodeSchema> =
471
663
  export type NodeBuilderData<T extends TreeNodeSchema> = T extends TreeNodeSchema<
472
664
  string,
473
665
  NodeKind,
474
- unknown,
666
+ TreeNode | TreeLeafValue,
475
667
  infer TBuild
476
668
  >
477
669
  ? TBuild
@@ -98,7 +98,7 @@ export function getStoredSchema(schema: TreeNodeSchema): TreeNodeStoredSchema {
98
98
  const kind = schema.kind;
99
99
  switch (kind) {
100
100
  case NodeKind.Leaf: {
101
- assert(schema instanceof LeafNodeSchema, "invalid kind");
101
+ assert(schema instanceof LeafNodeSchema, 0xa4a /* invalid kind */);
102
102
  return new LeafNodeStoredSchema(schema.info);
103
103
  }
104
104
  case NodeKind.Map: {
@@ -116,7 +116,7 @@ export function getStoredSchema(schema: TreeNodeSchema): TreeNodeStoredSchema {
116
116
  return new ObjectNodeStoredSchema(fields);
117
117
  }
118
118
  case NodeKind.Object: {
119
- assert(isObjectNodeSchema(schema), "invalid kind");
119
+ assert(isObjectNodeSchema(schema), 0xa4b /* invalid kind */);
120
120
  const fields: Map<FieldKey, TreeFieldStoredSchema> = new Map();
121
121
  for (const field of schema.flexKeyMap.values()) {
122
122
  fields.set(field.storedKey, convertField(field.schema));
@@ -726,6 +726,7 @@ function tryGetInnerNode(target: unknown): InnerNode | undefined {
726
726
  * Content which can be used to build a node.
727
727
  * @remarks
728
728
  * Can contain unhydrated nodes, but can not be an unhydrated node at the root.
729
+ * @system @alpha
729
730
  */
730
731
  export type FactoryContent =
731
732
  | IFluidHandle
@@ -744,12 +745,14 @@ export type FactoryContent =
744
745
  * Can contain unhydrated nodes, but can not be an unhydrated node at the root.
745
746
  *
746
747
  * Supports object and map nodes.
748
+ * @system @alpha
747
749
  */
748
- type FactoryContentObject = {
750
+ export type FactoryContentObject = {
749
751
  readonly [P in string]?: InsertableContent;
750
752
  };
751
753
 
752
754
  /**
753
755
  * Content which can be inserted into a tree.
756
+ * @system @alpha
754
757
  */
755
758
  export type InsertableContent = Unhydrated<TreeNode> | FactoryContent;
package/src/util/index.ts CHANGED
@@ -99,8 +99,14 @@ export type {
99
99
  RestrictiveStringRecord,
100
100
  _InlineTrick,
101
101
  FlattenKeys,
102
+ IsUnion,
103
+ UnionToIntersection,
104
+ UnionToTuple,
105
+ PopUnion,
102
106
  } from "./typeUtils.js";
103
107
 
108
+ export { unsafeArrayToTuple } from "./typeUtils.js";
109
+
104
110
  export {
105
111
  type BrandedKey,
106
112
  type BrandedKeyContent,
@@ -137,3 +137,90 @@ export type RestrictiveStringRecord<T> = {
137
137
  } & {
138
138
  readonly [P in symbol]?: never;
139
139
  };
140
+
141
+ /**
142
+ * Returns `true` if T is a union and false if it is not.
143
+ * @typeparam T - Type to test if it is a union.
144
+ * @typeparam T2 - Do not specify: default value used as implementation detail.
145
+ * @system @alpha
146
+ */
147
+ export type IsUnion<T, T2 = T> = T extends unknown
148
+ ? [T2] extends [T]
149
+ ? false
150
+ : true
151
+ : "error";
152
+
153
+ /**
154
+ * Convert a union of types to an intersection of those types. Useful for `TransformEvents`.
155
+ * @privateRemarks
156
+ * First an always true extends clause is used (T extends T) to distribute T into to a union of types contravariant over each member of the T union.
157
+ * Then the constraint on the type parameter in this new context is inferred, giving the intersection.
158
+ * @system @public
159
+ */
160
+ export type UnionToIntersection<T> = (T extends T ? (k: T) => unknown : never) extends (
161
+ k: infer U,
162
+ ) => unknown
163
+ ? U
164
+ : never;
165
+
166
+ /**
167
+ * Gets the first item of a union type.
168
+ *
169
+ * @typeparam Union - The union to convert.
170
+ * @typeparam AsOverloadedFunction - Implementation detail: do not specify.
171
+ * @system @alpha
172
+ */
173
+ export type PopUnion<
174
+ Union,
175
+ AsOverloadedFunction = UnionToIntersection<
176
+ Union extends unknown ? (f: Union) => void : never
177
+ >,
178
+ > = AsOverloadedFunction extends (a: infer First) => void ? First : never;
179
+
180
+ /**
181
+ * Converts a union type to a tuple type.
182
+ *
183
+ * @typeparam T - The union to convert.
184
+ * @typeparam A - Implementation detail: do not specify.
185
+ * @typeparam First - Implementation detail: do not specify.
186
+ *
187
+ * @privateRemarks
188
+ * `A` is a tuple to prepend members of the union to.
189
+ *
190
+ * https://www.hacklewayne.com/typescript-convert-union-to-tuple-array-yes-but-how and https://catchts.com/union-array both explain the general approach this uses pretty well.
191
+ * This implementation is inspired to those, but slightly different in implementation.
192
+ * @alpha
193
+ */
194
+ export type UnionToTuple<
195
+ Union,
196
+ A extends unknown[] = [],
197
+ First = PopUnion<Union>,
198
+ > = IsUnion<Union> extends true
199
+ ? UnionToTuple<Exclude<Union, First>, [First, ...A]>
200
+ : [Union, ...A];
201
+
202
+ /**
203
+ * This is unsafe, meaning that the returned value might not match its type.
204
+ *
205
+ * For the result to match its type, T must be a union of the types of each item in `items` in the order that they occur.
206
+ * For this to be possible, there must be no duplicate or overlapping types.
207
+ * This is fragile and must be used with care.
208
+ *
209
+ * @remarks
210
+ * The main use-case for this is recovering lost information from {@link AllowedTypes} which is necessary to preserve when using them with {@link Input} APIs.
211
+ * Since {@link AllowedTypes} is actually order independent, it is somewhat safer when used to produce `AllowedTypes`.
212
+ *
213
+ * @example
214
+ * ```typescript
215
+ * // Gives imprecise type (typeof A | typeof B)[]
216
+ * const schemaBad = [A, B];
217
+ * // Fixes the type to be [typeof A, typeof B]
218
+ * const schema = unsafeArrayToTuple(schemaBad);
219
+ *
220
+ * const config = new TreeViewConfiguration({ schema });
221
+ * ```
222
+ * @alpha
223
+ */
224
+ export function unsafeArrayToTuple<T>(items: T[]): UnionToTuple<T> {
225
+ return items as UnionToTuple<T>;
226
+ }