我想编写一个函数,该函数返回扩展的泛型类型的属性。甚至有可能吗?
这是我尝试过的:
interface Animal {
readonly weight: {total: number}
}
interface Dog extends Animal {
readonly weight: {total: number, tail: number, head: number, body: number }
}
const getWeight = <T extends Animal>(animal: T) => {
return animal.weight
}
const myDog = { weight: { total: 20, tail: 5, head: 5, body: 10 } }
const myDogsWeight = getWeight<Dog>(myDog)
// Property 'head' does not exist on type '{ total: number; }'.
const myDogsHeadWeight = myDogsWeight.head
如果尝试显式注释函数的返回类型,则会收到另一个错误:
type InferredWeight<TAnimal> = TAnimal extends { readonly weight: infer U } ? U : never
type DogWeight = InferredWeight<Dog> // <-- This type works correctly
const getWeightTyped = <T extends Animal>(animal: T): InferredWeight<T> => {
// Type '{ total: number; }' is not assignable to type 'InferredWeight<T>'.
return animal.weight
}
看起来唯一相关的TypeScript Github问题是this one,但这是联合类型的问题。
答案 0 :(得分:0)
如@jcalz在评论中所述,您可以使用查找类型:
这应该可以满足您的需求。
interface Animal {
readonly weight: { total: number }
}
interface Dog extends Animal {
readonly weight: { total: number, tail: number, head: number, body: number }
}
const getWeight = <
T extends Animal
>(animal: T): T['weight'] => {
return animal.weight
}
const myDog = { weight: { total: 20, tail: 5, head: 5, body: 10 } }
const myDogsWeight = getWeight(myDog);