Last active
December 12, 2019 02:24
-
-
Save willin/15823f752bf5cd9a29e783aafcc74959 to your computer and use it in GitHub Desktop.
proxy.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//定义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