我试图用io-ts定义一个新的编解码器。
完成后,形状应如下所示:
type General = unknown;
type SupportedEnv = 'required' | 'optional'
type Supported = {
required: General;
optional?: General;
}
(注意:目前General的形状并不重要)
这里的关键是我想基于General和SupportedEnv派生类型。
目前,我有以下内容:
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>;
类型Supported需要两个键:
type Supported = {
required: General;
optional: General;
}
我怎样才能使optional确实是可选的?
我尝试过使用交叉点和局部。。。但我不能用iots.record来理解语法。
这可能吗?我需要用不同的方式来思考吗?