【发布时间】:2021-02-17 12:03:21
【问题描述】:
对于这样一个笼统的问题标题,我深表歉意,但我真的不知道如何简短地描述我的问题。
情况如下:
type Data = {
id: number
name: string
}
function func(): Partial<Data> {
return { name: '' } // ok
}
function wrap<T extends Data>() {
function func(): Partial<T> {
return { name: '' } // Type '{ name: ""; }' is not assignable to type 'Partial<T>'
}
}
第二种情况的错误对我来说完全是个谜。
据我所知,the extends in the function constains T to be a subtype of the specified type。而且,据我所知——无论我的Data 类型的子类型是什么,它必须 有id: number 和name: string,对吗?
如果是,那么 { name: '' } 和 Partial<T> 有什么问题?
【问题讨论】:
-
为了简化您的示例,如果您删除包装器,问题仍然存在:
function funcExtends<T extends Data>(): Partial<T> { return { name: '' } }
标签: typescript