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

时间:2019-11-24 02:21:08

标签: typescript extends typescript-generics

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

这是我尝试过的:

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,但这是联合类型的问题。

1 个答案:

答案 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);