Discriminated unions with common field but different constraints
Hello,
I am having trouble setting up a schema-system with my types. My code is type-first, so the schemas must be written in accordance to my types.
My entities have a
type
property, which should be the discriminating property. Two types are possible: typeA
and typeB
. Both types of entities have a field named someField
. By default, someField
is nullable, meaning new types in the future should have it nullable. But typeA
entities require someField
not to be null.
Creating the corresponding schemas lead to a type error when trying to match my types to the schema's types.
Here is my code:
types.ts
schema.ts
1 Reply
myFormComponent.tsx
The discriminant JSON looks correct:
It seems to be related to the fact that
someField
is nullable. If I remove | null
from the type definition, the type error disappears.
My actual entities have nested fields and subtypes with multiple common fields that have different requirements. Hence, is this the wrong way to define the schemas ?