如何创建Partial<T>
类型,不允许使用undefined
值?这里是一个例子:
interface MyType {
foo: string
bar?: number
}
const merge = (value1: MyType, value2: KindaPartial<MyType>): MyType => {
return {...value1, ...value2};
}
const value = {
foo: 'foo',
bar: 42
}
merge(value, {}); // should work
merge(value, { foo: 'bar' }); // should work
merge(value, { bar: undefined }); // should work
merge(value, { bar: 666 }); // should work
merge(value, { foo: '', bar: undefined }); // should work
merge(value, { foo: '', bar: 666 }); // should work
// now the problematic case:
merge(value, { foo: undefined }); // this should throw an error
// because MyType["foo"] is of type string
我正在寻找的类型应该:
Partial<T>
一样]undefined
,则不接受undefined
这可能吗?如何创建kinda-Partial
类型,不允许使用未定义的值?这是一个示例:interface MyType {foo:string bar ?: number} const merge =(value1:MyType,value2:...