-
Notifications
You must be signed in to change notification settings - Fork 537
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Description The alpha enum APIs had some typing issues where they were unioning things at the wrong levels. Additionally since their tests predated unhydrated node support, they didn't make as many nodes as they probably should have. The tests have been simplified to use unhydrated nodes in a few places and extended to cover more cases. More explicit type testing has been added. This fixes an issue reported in #23055 See changeset for details. ## Breaking Changes See changeset.
- Loading branch information
1 parent
3aff19a
commit cfb6838
Showing
5 changed files
with
304 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
--- | ||
"fluid-framework": minor | ||
"@fluidframework/tree": minor | ||
--- | ||
--- | ||
"section": tree | ||
--- | ||
|
||
Fix typing bug in `adaptEnum` and `enumFromStrings` | ||
|
||
When using the return value from [`adaptEnum`](https://fluidframework.com/docs/api/v2/tree#adaptenum-function) as a function, passing in a value who's type is a union no longer produced an incorrectly typed return value. This has been fixed. | ||
|
||
Additionally [`enumFromStrings`](https://fluidframework.com/docs/api/v2/tree#enumfromstrings-function) has improved the typing of its schema, ensuring the returned object's members have sufficiently specific types. | ||
Part of this improvement was fixing the `.schema` property to be a tuple over each of the schema where it was previously a tuple of a single combined schema due to a bug. | ||
|
||
One side-effect of these fixes is that narrowing of the `value` field of a node typed from the `.schema` behaves slightly different, such that the node type is now a union instead of it being a single type with a `.value` that is a union. | ||
This means that narrowing based on `.value` property narrows which node type you have, not just the value property. | ||
This mainly matters when matching all cases like the switch statement below: | ||
|
||
```typescript | ||
const Mode = enumFromStrings(schema, ["Fun", "Bonus"]); | ||
type Mode = TreeNodeFromImplicitAllowedTypes<typeof Mode.schema>; | ||
const node = new Mode.Bonus() as Mode; | ||
|
||
switch (node.value) { | ||
case "Fun": { | ||
assert.fail(); | ||
} | ||
case "Bonus": { | ||
// This one runs | ||
break; | ||
} | ||
default: | ||
// Before this change, "node.value" was never here, now "node" is never. | ||
unreachableCase(node); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.