打字稿对象包装,打字和泛型 [英] Typescript object wrapping, typings and generics

查看:128
本文介绍了打字稿对象包装,打字和泛型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有点像是我昨天在其他地方发布的问题的转变.

This is somewhat the transformation of a question I posted somewhere else yesterday.

我的目标不是获得可行的结果,而是更好地理解在返回正确的类型时可以得到的设计类型.通过使用一个极简主义的示例,所以请不要告诉我这是无用的或什么也不做.

My objective is not to get a working result but to have a better understanding of the kind of design I can obtain while returning the correct types. This by using here a minimalist example, so please don't tell me it's useless or does nothing.

代码示例(此处:
w具有类型Wrapper<Obj>
r1具有类型{test: string}
w.test具有类型Wrapper<Obj>.test: () => {}
r2具有类型{}
r3具有类型{}

Here:
whas the type Wrapper<Obj>
r1 has the type {test: string}
w.test has the type Wrapper<Obj>.test: () => {}
r2 has the type {}
r3 has the type {}

这种情况是一个将对象存储在属性中的类.它代理对此对象的调用.

This case is a class that stores an object in a property. It proxies the calls to this object.

我感兴趣的是r2的返回类型.特别是我想找到一种方法来返回与r1相同的类型,而无需在三行底行中指定类型的事实. r3是另一个测试,通过直接传递泛型类型.结果是一样的.

What interest me is the return type of r2. And specifically the fact that I would like to find a way to return the same type as for r1 without specifying the type in the three bottom lines. r3is another test, by passing directly the generic type. The result is the same.

对此我有一些疑问:

  • 据我了解,Wrapper.test返回类型在OBJ泛型具有任何影响之前就已解决.因此使用其基值(在本例中为{},这是TestInterface.Test的结果).是吗?
  • 这是否是有目的的,某些限制的功能,以及将来的TS版本中即将推出的功能? (对此一无所获)
  • 并且主要是,考虑到在这样的代理中,应该有可能插入任何尊重TestInterface的东西,因此如何转发嵌入式对象方法的返回类型(或使用它们组成新的对象).而且无需将泛型放在调用方的所有位置(最底端),我知道该怎么做.
  • From my understanding the Wrapper.test return type is resolved before the OBJ generic has any influence. So its base value is used (in this case {} which is the result of TestInterface.Test). Is it right?
  • Is it something done on purpose, some limitation, some upcoming feature in future TS versions? (didn't see anything about that)
  • And mainly, how to be able to forward embedded object methods return types (or compose a new object with them), considering that in such a proxy, it should be possible to plug anything that respect the TestInterface. And without putting generics everywhere in the caller (at the bottom lines), that I know how to do.

我在这篇文章中看到了一些对象生成器: https://github.com/Microsoft/TypeScript/pull/14141 .也许这就是我要走的方向.

I've seen some object builder in this post: https://github.com/Microsoft/TypeScript/pull/14141. Maybe that's the direction I'll take.

推荐答案

我认为是您想要的:

interface TestInterface<T1,T2> { 
    test: () => T1
    test2: () => T2
}

class Obj implements TestInterface<{ test: string }, { test2: number }>{
    test() { 
        return { test: 'test' }
    }

    test2() { 
        return { test2: 2 }
    }
}

class Wrapper<T1,T2> implements TestInterface<T1,T2>  {   
    public obj :TestInterface<T1,T2>;
    constructor(obj: { new (): TestInterface<T1,T2> }) {
        this.obj = new obj();
    }

    test():T1 { 
        return this.obj.test();
    }

    test2():T2 { 
        return this.obj.test2();
    }
}

let w = new Wrapper(Obj);
let r = w.test();
let s = w.test2();

这篇关于打字稿对象包装,打字和泛型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆