TS2339: Property does not exist on type {}

折月煮酒 提交于 2020-01-03 01:28:21

问题


Please help me fix this compilation error.

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 {}:

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):

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?


回答1:


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

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);

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.



来源:https://stackoverflow.com/questions/34179728/ts2339-property-does-not-exist-on-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!