Skip to content

Instantly share code, notes, and snippets.

@willin
Last active December 12, 2019 02:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willin/15823f752bf5cd9a29e783aafcc74959 to your computer and use it in GitHub Desktop.
Save willin/15823f752bf5cd9a29e783aafcc74959 to your computer and use it in GitHub Desktop.
proxy.d.ts
//定义proxy中必须实现的handler参数接口
interface ProxeeHandler<T extends object, TOut extends object> {
get?<K extends keyof TOut>(target: T, p: K, receiver: TOut): TOut[K];
set?<K extends keyof TOut>(target: T, p: K, value: TOut[K], receiver: TOut): boolean;
}
//定义proxy的构造函数接口
interface ProxeeConstructor {
new <T extends object, TOut extends object>(target: T, handler: ProxeeHandler<T, TOut>): TOut;
}
//用以上两个接口声明一个Proxee,其用法和Proxy一模一样
declare var Proxee: ProxeeConstructor;
//用法如下:
let obj = {
prop1: function () { },
prop2: 'hello',
}
let prox: Record<keyof typeof obj, number> = new Proxee(obj, {
get(target, name) {
return 5;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment