0

Hi I'm coding an npm package, is coded in typescript and transpiled.

That package is now been used in a typescript app, but I have 1 error:

[ts]
Argument of type 'import("/home/chriss/Sites/ORM/connection").DB' is not assignable to parameter of type 'import("/home/chriss/Sites/ORM/dist/connection").DB'.
Property 'connections' is protected but type 'DB' is not a class derived from 'DB'.

Is the same Class just 1 is the typescript one and the other one is the transpiled one

I declare like this:

import { DB } from "@unicoderns/orm/connection"
...
constructor(config: Config, baseURL: string) {
    this.config = config;
    this.db = new DB({ dev: config.dev, connection: config.dbconnection });
}

And then call the model like this:

this.usersTable = new users.Users(jsloth.db);

where jsloth.db is the this.db from the first code.

And this is in the npm package whats expecting:

constructor(DB: DB, privacy?: string);

What can I do?

EDIT 1:

Package already released, source available at:

https://github.com/unicoderns/ORM

The dirty and quick workaround was add | any to the type expected model.ts at line 56 constructor(DB: DB | any

This should be corrected :)

The library consuming this package is also OpenSource and the code can be found at:

https://github.com/unicoderns/JSloth

Once you remove the | any from the package several files should "yell" the error in the IDE but probably still works, like source/system/apps/auth/endpoint/v1/index at lines 57 and 58

Thanks for the help again.

Chriss Mejía
  • 110
  • 1
  • 10
  • 1
    Somehow you are importing both `/home/chriss/Sites/ORM/connection` and `/home/chriss/Sites/ORM/dist/connection` in the app. Pick one. If you add more of your code and your `tsconfig.json` file to the question, I can help you figure out how the problem arose. – Matt McCutchen Sep 18 '18 at 02:20
  • Thank you so much @MattMcCutchen, I edited the question to include the requested information. – Chriss Mejía Sep 18 '18 at 02:31

1 Answers1

1

If you point the "main" and "typings" fields in package.json of the @unicoderns/orm package to the dist subdirectory, then an import of @unicoderns/orm will go to that subdirectory. When you import a non-main module of @unicoderns/orm, such as on source/system/lib/core.ts line 25:

import { DB } from "@unicoderns/orm/connection"

you need to use the path in dist to maintain consistency:

import { DB } from "@unicoderns/orm/dist/connection"

See this thread and particularly this comment for more information.

Matt McCutchen
  • 28,856
  • 2
  • 68
  • 75
  • Oh you are good, it makes sense, thank you so much for your time @MattMcCutchen, however, `"@unicoderns/orm/dist/connection"` seems unnatural to me, there's any workaround I can do to keep it like it is? (`"@unicoderns/orm/connection"`), any configuration I can chage, structure? I don't know. – Chriss Mejía Sep 18 '18 at 02:56
  • 1
    Your main options are described in the issue I linked: either change the `@unicoderns/orm` package so the `.js` and `.d.ts` files are generated alongside the `.ts` files, or set the `paths` compiler option on every project that uses `@unicoderns/orm` to map `@unicoderns/orm/foo` to `@unicoderns/orm/dist/foo`. – Matt McCutchen Sep 18 '18 at 03:00