请帮我解决这个编译错误。
下面您可以看到编译器提示第 20 行的 Actions 对象(为清楚起见,我在发布之前删除了几行)是 {}:
但是你可以在下面的 actions.ts 中看到 Actions 是一个 Actions 类型的对象,并且它具有请求的属性(是一个函数):
在基本代码中,您可以在 DefinitelyTyped Alt 定义中看到 createActions 应该返回一个类型为 Actions 的对象:
那么为什么 Typescript 会提示 Actions 不是 Actions 类型呢?
最佳答案
您正在使用一个名为 "app/actions/actions"
的模块.该模块实际上不是一个模块(属性映射),但是 flux.createACtions(Actions)
的结果是什么? :
export = flux.createActions(Actions); // in actions.ts
返回什么?因为您没有为 <T>
指定泛型,并且因为 createActions
的参数没有正确包含 T
从中可以推断出,它假设 T
只是{}
.这是 discussed here并最终拒绝。因此,如前所述,您需要指定泛型:
export = flux.createActions<Actions>(Actions);
但要避免这种情况,您可以更改本地(或远程)alt.d.ts
是这样的:
class Alt {
createActions<T extends ActionsClass>(con: ActionsClassConstructor<T>, ...): T;
}
type ActionsClassConstructor<T extends ActionsClass> = new (alt:Alt) => T;
这会添加根据您提供的构造函数正确推断所需的通用类型信息。
关于typescript - TS2339 : Property does not exist on type {},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34179728/