打字稿:找不到名称“代理" [英] Typescript: Cannot find name 'Proxy'

查看:86
本文介绍了打字稿:找不到名称“代理"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要声明一个类型为Proxy的新变量,该变量来自ES6规范:

I need to declare a new variable with type Proxy, that is from ES6 specification:

myProxy: Proxy;

但是我遇到下一个错误:

But I get the next error:

找不到名称代理".

Cannot find name 'Proxy'.

我该如何解决?

推荐答案

假定您已将目标设置为es2015或使用lib选项包含了es2015的库,则可以创建Proxy .诀窍是Proxy不是类型,而是构造函数.来自es2015 lib:

Assuming you have the target set to es2015 or are including the lib for es2015 using the lib option, you can create a Proxy. The trick is that Proxy is not a type, it's a constructor. From the es2015 lib:

interface ProxyConstructor {
    revocable<T extends object>(target: T, handler: ProxyHandler<T>): { proxy: T; revoke: () => void; };
    new <T extends object>(target: T, handler: ProxyHandler<T>): T;
}
declare var Proxy: ProxyConstructor;

因此,要创建代理,您需要调用构造函数:

So to create a proxy you need to invoke the constructor:

let foo = new Proxy({ value: 0 }, {
    get: (v) => v.value * 2
});

foo的类型将与目标对象的类型相同.

The type of foo will be the same as the type of the target object.

这篇关于打字稿:找不到名称“代理"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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