thr*_*eve 10 typescript es6-proxy angular
我有一个抽象类:
abstract class Foo {
abstract bar(): string;
}
Run Code Online (Sandbox Code Playgroud)
我有一些扩展的类Foo:
class Foo1 extends Foo {
bar(): string { return 'foo1'; }
}
class Foo2 extends Foo {
bar(): string { return 'foo2'; }
}
Run Code Online (Sandbox Code Playgroud)
我还有一个类,我要代理的所有方法Foo的Foo。这实际上工作正常,如果我Foo在这个类上定义所有方法。但我宁愿不这样做。我宁愿让Foo定义的方法Foo和编译器知道FooProxy也实现了这些方法,而不必实际实现它们。这可能吗?Proxy 类看起来像这样:
class FooProxy {
public foo: Foo;
constructor(foo: Foo) {
this.foo = foo;
let handler = {
get: function(target: FooProxy, prop: string, receiver: any) {
if(Foo.prototype[prop] !== null) {
return target.foo[prop];
}
return Reflect.get(target, prop, receiver);
}
}
return new Proxy(this, handler);
}
}
Run Code Online (Sandbox Code Playgroud)
例子:
let f = new Foo1();
let fp = new FooProxy(f);
fp.bar();
Run Code Online (Sandbox Code Playgroud)
输出:
error TS2339: Property 'bar' does not exist on type 'FooProxy'.
Run Code Online (Sandbox Code Playgroud)
这个程序实际上在操场上运行,但tsc不发出任何东西。我只需要欺骗编译器一些如何......
Tit*_*mir 12
我不认为在这种情况下,类是最好的方法,您可以仅使用函数来创建代理,并且一切都会按预期工作:
function createFooProxy(foo:Foo) : Foo { // Proxy<Foo> is compatible with Foo
let handler = {
get: function(target: Foo, prop: keyof Foo, receiver: any) {
if(Foo.prototype[prop] !== null) {
return foo[prop];
}
return Reflect.get(target, prop, receiver);
}
}
return new Proxy(foo, handler);
}
Run Code Online (Sandbox Code Playgroud)
如果你开始使用类方法,你可以伪造一个基类:
function fakeBaseClass<T>() : new() => Pick<T, keyof T>{ // we use a pick to remove the abstract modifier
return class {} as any
}
class FooProxy extends fakeBaseClass<Foo>(){
private foo: Foo; // I would make this private as it is not really accessible on what the constructor of FooProxy returns (maybe remove it as I see no use for it)
constructor(foo: Foo) {
super();
this.foo = foo;
let handler = {
get: function(target: FooProxy, prop: keyof Foo, receiver: any) {
if(Foo.prototype[prop] !== null) {
return target.foo[prop];
}
return Reflect.get(target, prop, receiver);
}
}
return new Proxy(this, handler);
}
}
Run Code Online (Sandbox Code Playgroud)
作为对已接受答案的补充,我想分享一个解决方案,该解决方案可用于更简单的情况,您只希望能够代理任何方法或属性(处理程序中的陷阱或实际存在的某些内容)目标)。该解决方案基于使用“索引签名”,请在此处阅读有关该主题的更多信息:https : //www.typescriptlang.org/docs/handbook/interfaces.html
我希望它对其他人在这个好问题上有所帮助:
const handler = {
get: (target: object, property: string, receiver: any) => {
// define custom traps here:
if (property === "trap") {
// ... do something ...
}
// optional test whether property exists using the Reflect class:
if (!Reflect.has(target, property)) {
throw Error(`Property ${property} does not exist`);
}
// proxy to the target using the Reflect class:
return Reflect.get(target, property);
},
};
Run Code Online (Sandbox Code Playgroud)
我将展示两种解决方案:
1.创建一个工厂和一个接口:
// Any property is allowed in this interface using the following index signature:
interface FooInterface {
[key: string]: any;
}
// From the factory we return the FooInterface
const proxyFactory = (target: Foo): FooInterface => {
return new Proxy(target, handler);
};
Run Code Online (Sandbox Code Playgroud)
2.创建一个带有索引签名的类,并直接从类构造函数中返回代理
class FooProxy {
[key: string]: any;
constructor(target: Foo) {
return new Proxy(target, handler);
}
}
Run Code Online (Sandbox Code Playgroud)
可以像这样使用,您要在处理程序内部构建陷阱的所有可能的方法和属性都将被识别:
const fooProxy = proxyFactory(foo);
Run Code Online (Sandbox Code Playgroud)
或者
const fooProxy = new FooProxy(foo);
Run Code Online (Sandbox Code Playgroud)
没有更多的消息抱怨:
类型“代理”上不存在属性“ ...属性名称... ”。
所以现在可以调用和捕获任何东西:
const returnedFromMethod = fooProxy.anyMethod();
const property = fooProxy.anyProperty;
Run Code Online (Sandbox Code Playgroud)
您当然可以优化您的界面,该示例只是为了演示所有方法和属性的解决方案。
| 归档时间: |
|
| 查看次数: |
7986 次 |
| 最近记录: |