krislefeber / nestjs-dataloader

Dataloader plugin for NestJS
https://krislefeber.github.io/nestjs-dataloader/
MIT License
148 stars 45 forks source link

Property 'generateDataLoader' in type ... is not assignable to the same property in base type #27

Open andreialecu opened 4 years ago

andreialecu commented 4 years ago

The project I'm trying to use nestjs-dataloader in uses import DataLoader from "dataloader"; syntax for imports, via the esModuleInterop: true flag in tsconfig.json

This seems to conflict with the way DataLoader is imported here: https://github.com/krislefeber/nestjs-dataloader/blob/master/index.ts#L11

And it results in errors such as:

error TS2416: Property 'generateDataLoader' in type 'CanEditMatchdayLoader' is not assignable to the same property in base type 'NestDataLoader<string, Matchday>'.
  Type '() => import("/home/aandrei/projects/projectsport-server/node_modules/dataloader/index")<string, import("/home/aandrei/projects/projectsport-server/src/matchday/matchday.model").Matchday, string>' is not assignable to type '() => DataLoader<string, import("/home/aandrei/projects/projectsport-server/src/matchday/matchday.model").Matchday, string>'.
    Call signature return types 'DataLoader<string, Matchday, string>' and 'DataLoader<string, Matchday, string>' are incompatible.
      The types returned by 'clear(...)' are incompatible between these types.
        Type 'DataLoader<string, Matchday, string>' is not assignable to type 'this'.

11   generateDataLoader(): DataLoader<string, Matchday> {

I have confirmed that changing the tsconfig of nestjs-dataloader to enable esModuleInterop and using the same of type of import results in the error being fixed.

I'm not sure what the proper fix is here, but I think as a best practice, the typescript file should not be published to npm, but instead only publish the d.ts and compiled .js. It's likely that the index.ts file is interfering somehow.

andreialecu commented 4 years ago

Related: #6

hboylan commented 4 years ago

Is there a workaround for this? I'm stuck using esModuleInterap to support other modules.

Casting generateDataLoader(): any squashes the typescript error, but results in the loader failing to properly load.

Nest could not find MyLoader element (this provider does not exist in the current context)
andreialecu commented 4 years ago

I personally used https://www.npmjs.com/package/patch-package to replace this line: https://github.com/krislefeber/nestjs-dataloader/blob/df8fa93897389ee692e9137c6beb7629f81d86c3/index.ts#L11 with import DataLoader from 'dataloader';

hallettj commented 4 years ago

Currently this package distributes the index.ts source file, which is what TypeScript picks up for the package's type definitions. I think this issue might be fixed by switching to distributing a generated index.d.ts definition file instead.