给定两个接口
// the "normal" state
interface State {
index: number;
offset: number;
}
// let's extend it with some additional fields
interface SpecialState extends State {
search: string;
filter: string;
}
我正在try 创建一个函数,该函数将接受SpecialState类型的分部对象作为参数,其中must包括扩展属性,还可以包括基类型的任何属性.因此,输入类型应该是这样的
type InitialState<T extends State> = Partial<State> & Omit<T, keyof State>;
这很好用,我可以创建一个对象,其中search
和filter
是必需的,limit
和offset
是可选的.
const initialSpecialState: InitialState<SpecialState> = {
search: 'hello',
filter: 'bonjour',
index: 2
};
该函数的结果将是扩展类型的完整对象(在本例中为SpecialState).也就是说,该函数将"知道"基本接口,并能够为这些属性生成默认值,以防它们在输入中丢失.但它不能为扩展属性生成任何缺省值,因为它们是未知的.因此,它们需要在输入中给出.
因此,该函数将是泛型的,并将基类型State
的任何扩展作为类型参数.
function makeFinalState<T extends State>(initialState: InitialState<T>): T {
const baseState: State = {
index: 0,
offset: 0,
}
// TS doesn't like this
const mergedState: T = {
...baseState,
...initialState
}
return mergedState
};
这就是TypeScrip向我抛出错误的地方
'{ index: number; offset: number; } & Partial<State> & Omit<T, keyof State>' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'State'
但是,该函数在我的用例中工作得很好,而不考虑错误
const finalState = makeFinalState<SpecialState>(initialSpecialState);
console.log(finalState)
// logs
// {
// "index": 2,
// "offset": 0,
// "search": "hello",
// "filter": "bonjour"
// }
那么这里出了什么问题呢?或者我应该以一种完全不同的方式来做这件事?