Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Typescript enforces invariance in generics which means that the
useSharedFacet
requires a single specific type. This leads to type errors when trying to pass a union, e.g:However, with the changes I'm introducing here the hook now leverages conditional types with infer to automatically distribute over unions. So when a union of shared facets is passed to
useSharedFacet
, the return type becomes a union of the corresponding facet types. Which means that theregularFacetUnion
above will be of typeFacet<CreateNewWorldFacet> | Facet<WorldEditorFacet> | Facet<RealmWorldEditorFacet>
.We are then able to use that facet union with our other hooks like
useFacetMap
:One downside is that the updated implementation now uses a type assertion (
as InferFacet<T>
) because typescript cannot automatically verify that the value returned bysharedFacet(driver)
conforms to the conditional type. However, I believe that this cast is always safe based on the functions behaviour.