I am currently in the process of defining a new codec using io-ts
.
Once completed, I want the structure to resemble the following:
type General = unknown;
type SupportedEnv = 'required' | 'optional'
type Supported = {
required: General;
optional?: General;
}
(Please note that at this time, the specific form of General
is not crucial)
The main objective is to generate the type based on both General
and SupportedEnv
.
My current implementation looks something like this:
const GeneralCodec = iots.unknown;
const SupportedEnvCodec = iots.union([
iots.literal('required'),
iots.literal('optional'),
]);
const SupportedCodec = iots.record(SupportedEnvCodec, GeneralCodec)
type Supported = iots.TypeOf<typeof SupportedCodec>;
The type Supported
requires keys for both mandatory and optional fields:
type Supported = {
required: General;
optional: General;
}
Is there a way to actually make the optional
field truly optional?
I have experimented with intersections and partial types...however, I am struggling with the syntax when incorporating them into iots.record
.
Could this be accomplished? Should I approach this situation from a different angle?