繁体   English   中英

是否可以从Typescript中的非构造函数方法参数推断出通用类类型?

[英]Is it possible to infer a generic class type from a non-constructor method parameter in Typescript?

A-构造方法案例

当我想从构造函数方法参数推断通用类类型时:

interface ObjectAsMap { [key: string]: boolean }

interface MyClass<T extends ObjectAsMap> {
  data: T

  constructor(data: T): MyClass<T>
}

class MyClass<T extends ObjectAsMap> {
  constructor(data: T) {
    this.data = data
  }
}

const a = new MyClass('Wrong parameter type')
const b = new MyClass({
  first: true,
  second: false,
})

console.log(b.data.first)
console.log(b.data.wrongProperty)

不出所料 ,我收到2个错误

  • new MyClass('Wrong parameter type')触发器Argument of type '"Wrong parameter type"' is not assignable to parameter of type 'ObjectAsMap'.
  • b.data.wrongProperty触发器Property 'wrongProperty' does not exist on type '{ first: true; second: false; }'. Property 'wrongProperty' does not exist on type '{ first: true; second: false; }'.

B-非构造方法案例

现在,如果我想从非构造方法中触发完全相同的预期行为:

interface ObjectAsMap { [key: string]: boolean }

interface MyClass<T extends ObjectAsMap> {
  data: T

  declare(data: T): MyClass<T>
}

class MyClass<T extends ObjectAsMap> {
  public data: T

  public declare(data: T) {
    this.data = data

    return this
  }
}

const myClassInstance = new MyClass()

const a = myClassInstance.declare('Wrong parameter type')
const b = myClassInstance.declare({
  first: true,
  second: false,
})

console.log(b.data.first)
console.log(b.data.wrongProperty)

我只得到第一个错误

  • myClassInstance.declare('Wrong parameter type')触发器Argument of type '"Wrong parameter type"' is not assignable to parameter of type 'ObjectAsMap'.

由于b#data中不存在此属性,因此b.data.wrongProperty也应触发错误。 当我将鼠标悬停在b.data上方b.data ,它告诉我(property) MyClass<ObjectAsMap>.data: ObjectAsMap而不是(property) MyClass<{ first: true; second: false; }>.data: { first: true; second: false; } (property) MyClass<{ first: true; second: false; }>.data: { first: true; second: false; } (property) MyClass<{ first: true; second: false; }>.data: { first: true; second: false; }


有没有办法像情况A那样推断情况B中的参数类型?

您只需要添加一个额外的type参数即可捕获调用中的实际data类型

interface ObjectAsMap { [key: string]: boolean }

interface MyClass<T extends ObjectAsMap> {
    data: T

    declar<U extends T>(data: U): MyClass<U>
}

class MyClass<T extends ObjectAsMap> {
    public data: T

    public declare<U extends T>(data: U): MyClass<U> {
        this.data = data

        return this as any
    }
}

const myClassInstance = new MyClass()

const a = myClassInstance.declare('Wrong parameter type')
const b = myClassInstance.declare({
    first: true,
    second: false,
})

console.log(b.data.first)
console.log(b.data.wrongProperty)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM