Is there a standard pattern within a monolithic Typescript Interface or Type definitions to assert properties either appear together or don't appear at all?
For example an item could be valid if it looked like this...
{
id:"ljklkj",
spellcheck:true,
spellcheckModel:"byzantine",
}
...or this...
{
id:"ljklkj",
}
However it would be invalid if either of the spellcheck properties occurred in isolation.
{
id:"ljklkj",
spellcheckModel:"byzantine",
}
{
id:"ljklkj",
spellcheck:true,
}
Monolithic
Of course the simple case above could be resolved by creating a Data and a SpellcheckData Type or Interface. In my application case, however, there will be more than one 'cluster' of co-occurring properties. Defining a new type for every combination of co-occurrence would lead to an explosion of types in order to express the case.
For this reason I've referred to the solution as a 'monolithic' interface. Of course it may be necessary to use some form of composition to define it.
What I've Tried
I have tried to find examples like this within the Typescript language reference, but without knowing what the feature might be called, (or indeed if it's a feature that can be expressed at all), I'm struggling. Properties can be individually optional but I can't see a way of expressing co-occurrence.
Related Technologies
An equivalent feature for XML data validation is discussed here... https://www.w3.org/wiki/Co-occurrence_constraints
For JSON I understand schema languages like Schematron and Json Content Rules are able to express co-constraints.
Worked example
If I were to imagine typescript syntax for the co-constraint case applied to HTTP parameter sets for the Solr search engine, it might look like this, indicating that you could opt in to fully satisfying the Spell or Group params, or not at all - a union in which each type is optional (indicated by the ?) ...
type SolrPassthru =
SolrCoreParams & (
SolrSpellParams? |
SolrGroupParams?
)
This contrasts with the example below, which is I believe is correct Typescript, but requires ALL parameters from each group of parameters.
type SolrCoreParams = {
defType: SolrDefType,
boost: SolrBoostType,
}
type SolrSpellParams = {
spellcheck: "true" | "false",
"spellcheck.collate": "true" | "false",
"spellcheck.maxCollationTries": 1,
}
type SolrGroupParams = {
group: "true" | "false",
"group.limit": '4'
"group.sort": 'group_level asc,score desc,published desc,text_sort asc'
"group.main": 'true'
"group.field": 'group_uri'
}
type SolrPassthru =
SolrCoreParams &
SolrSpellParams &
SolrGroupParams