问题描述
这有点像是我昨天在其他地方发布的问题的转变.
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.
代码示例(尝试一下在Typescript游乐场中):
interface TestInterface {
test: () => {}
}
class Obj implements TestInterface{
test() {
return { test: 'test' }
}
}
class Wrapper<T extends TestInterface> {
constructor(public obj: T) {
}
test<T>() {
return this.obj.test();
}
test2<T extends TestInterface>(obj: T) {
return obj.test();
}
}
let w = new Wrapper(new Obj());
let r1 = w.obj.test();
let r2 = w.test();
let r3 = w.test2(new Obj);
此处:w
具有类型Wrapper<Obj>
r1
具有类型{test: string}
w.test
具有类型Wrapper<Obj>.test: () => {}
r2
具有类型{}
r3
具有类型{}
Here:w
has 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. r3
is 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 theOBJ
generic has any influence. So its base value is used (in this case{}
which is the result ofTestInterface.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();
这篇关于打字稿对象包装,打字和泛型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!