TS2339:类型{}上不存在属性 [英] TS2339: Property does not exist on type {}

查看:4996
本文介绍了TS2339:类型{}上不存在属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我修复此编译错误.

Please help me fix this compilation error.

在下面,您可以看到编译器抱怨第20行的 Actions 对象(为清楚起见,我删除了几行内容,然后发布了该行)是{}:

Below you can see the compiler complaining that the Actions object on line 20 (I removed a few lines for clarity before posting this) is {}:

但是在下面的 actions.ts 中,您可以看到 Actions Actions 类型的对象,它具有请求的属性(该属性是一个函数):

But below you can see in actions.ts that Actions is an object of type Actions, and it has the requested property (which is a function):

在基本代码中,您可以在DefinitelyTyped Alt 定义中看到, createActions 应该返回类型为 Actions 的对象:

And in the base code you can see in the DefinitelyTyped Alt definition that createActions should return an object of type Actions:

那么打字稿为什么抱怨动作不是动作类型?

So why is Typescript complaining that Actions is not of type Actions?

推荐答案

您正在使用名为"app/actions/actions"的模块.该模块实际上不是模块(属性映射),但是flux.createACtions(Actions)的结果是什么:

You're using a module called "app/actions/actions". That module is actually not a module (a map of properties), but whatever's the result of flux.createACtions(Actions):

export = flux.createActions(Actions); // in actions.ts

那又返回什么?由于您没有为<T>指定通用名称,并且由于createActions的参数没有正确包含可以从中推断出的T,因此假定T只是{}.这是在此处讨论,并最终被拒绝了.因此,如上所述,您需要指定泛型:

What does that return? Because you're not specifying the generic for <T>, and because the params of createActions don't correctly include a T from which it could infer, it assumes that T is just {}. This was discussed here and ultimately declined. So, as mentioned, you need to specify the generic:

export = flux.createActions<Actions>(Actions);

但是要避免这种情况,您可以将本地(或远程)alt.d.ts更改为:

But to avoid this, you could change your local (or remote) alt.d.ts to be something like:

class Alt {
    createActions<T extends ActionsClass>(con: ActionsClassConstructor<T>, ...): T;
}
type ActionsClassConstructor<T extends ActionsClass> = new (alt:Alt) => T;

这将根据您提供的构造函数添加正确推断所需的通用类型信息.

This adds the generic type info needed to correctly infer based on the constructor you supply.

这篇关于TS2339:类型{}上不存在属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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