Search code examples
typescriptooptsctranspiler

npm package with classes not compatible between typescript and transpiled versions


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.


Solution

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