如何推断扩展的泛型类型的子属性类型?

问题描述 投票:0回答:1

我想编写一个函数,该函数返回扩展的泛型类型的属性。可能吗?

这是我尝试过的:

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
}

这是playground link

似乎唯一相关的TypeScript Github问题是this one,但它是联合类型的问题。

typescript extends typescript-generics
1个回答
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);
© www.soinside.com 2019 - 2024. All rights reserved.