Sharing functionality
⚠️ Available starting with v10.3.0
Often, multiple documents have a shared shape, and it’s convenient to share functionality across them. For example, you might want a function that renames a user or a profile. For that purpose, Typesaurus provides a set of types and methods that enable sharing of functionality across the refs and docs in a type-safe way.
In this example, the as
method checks that the user extends NameField
. It would resolve unknown
if the user model weren’t compatible with NameFields
, triggering a type error:
On the function side, we use Typesaurus.SharedEntity
type that unions Typesaurus.SharedRef
and Typesaurus.SharedDoc
. These shared types narrow the data type to the given shape (i.e. NameFields
) and limit the available methods to the ones that don’t depend on the full type of the model. The set
, upset
, and as
methods are stripped to prevent data inconsistencies.
→ Read more about the as
method
→ Read more about the Typesaurus.Entity
type
→ Read more about the Typesaurus.Ref
type
→ Read more about the Typesaurus.Doc
type
→ Read more about the Typesaurus.Collection
type