当我使用扩散运算符时,TypeScrip不能识别对象的属性是否针对空/未定义进行判断.
interface MyObject {
date: SerializedDateTime;
someId: number;
}
interface MyObjectInput {
date?: Maybe<SerializedDateTime>;
someId?: Maybe<number>;
}
function useMyObject(myObject: MyObject) {
//Do something with myObject
}
function test(input: MyObjectInput) {
if (!input.date) {
throw new Error('Date not provided');
}
if (!input.someId) {
throw new Error('SomeId not provided');
}
//This works...
useMyObject({
date: input.date,
someId: input.someId,
});
//Here the compiler complains that the types are incompatible
useMyObject({
...input
});
}
确切的信息是:
Types of property 'date' are incompatible.
Type 'Maybe<Date>' is not assignable to type 'Date'.
Type 'undefined' is not assignable to type 'Date'
我不知道为什么会发生这种事.属性被判断为空/未定义,因此是兼容的,正如您可以看到的,当我不使用spad运算符时.有没有人知道这个问题的原因以及如何解决?