【问题标题】:Inference of Subtypes of Generic Types in TypeScriptTypeScript 中泛型类型的子类型推断
【发布时间】:2018-01-18 19:56:46
【问题描述】:

我想要一个函数,它接受一些对象并返回其x 属性。该对象需要限制为泛型类型Type<X>,并且我希望返回值的类型是属性x 的类型。

要将输入限制为Type<X>,我需要使用T extends Type<X>,但实际上我必须将X 设置为某种类型值,例如T extends Type<string>,它不适用于Type<number>T extends Type<any>丢弃x属性的类型信息。

我希望做类似<T extends Type<any>>(o: T) => T.X<T extends Type<???>>(o: T) => typeof o 之类的事情。

TypeScript 中有没有办法做到这一点?如果有,怎么做?

// Suppose I have this generic interface
interface Type<X> {
  readonly x: X
}

// I create one version for foo...
const foo: Type<string> = {
  x: 'abc',
}

// ...and another one for bar
const bar: Type<number> = {
  x: 123,
}

// I want this function to restrict the type of `o` to `Type`
// and infer the type of `o.x` depending on the inferred type of `o`,
// but to restrict to `Type` I must hardcode its X to `any`, which
// makes `typeof o.x` to evaluate to `any` and the type of `o.x` is lost.
function getX<T extends Type<any>> (o: T): typeof o.x {
  return o.x
}

// These are correctly typed
const okFooX: string = getX(foo)
const okBarX: number = getX(bar)

// These should result in error but it is OK due to `Type<any>`
const errorFooX: boolean = getX(foo)
const errorBarX: boolean = getX(bar)

【问题讨论】:

    标签: typescript type-inference typescript-generics


    【解决方案1】:

    如果我理解正确的话:

    function getX<T>(o: Type<T>): T {
        return o.x;
    }
    

    然后:

    const errorFooX: boolean = getX(foo); // error: Type 'string' is not assignable to type 'boolean'
    const errorBarX: boolean = getX(bar); // error: Type 'number' is not assignable to type 'boolean'
    

    (code in playground)

    【讨论】:

    • 是的,你没看错,这正是我想要达到的,但我没有想到。
    猜你喜欢
    • 2017-10-04
    • 2019-09-17
    • 1970-01-01
    • 2021-07-28
    • 2020-10-11
    • 2016-12-05
    • 2021-10-18
    • 1970-01-01
    • 2020-09-09
    相关资源
    最近更新 更多