Search code examples
typescriptfluxalt.js

TS2339: Property does not exist on type {}


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 {}: enter image description here

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): enter image description here

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

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


Solution

  • 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.