我试图用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
来理解语法。
这可能吗?我需要用不同的方式来思考吗?