Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types don't match module format #25

Closed
jefbarn opened this issue Oct 24, 2023 · 6 comments · Fixed by #26
Closed

types don't match module format #25

jefbarn opened this issue Oct 24, 2023 · 6 comments · Fixed by #26

Comments

@jefbarn
Copy link
Contributor

jefbarn commented Oct 24, 2023

Ran into a weird issue with this module after switching typescript to the 'node16' module format.
It boils down to the code is exporting a function as a cjs module, but the index.d.ts definitions are written as if it's esm.

error TS2349: This expression is not callable.
Type 'typeof import(".../node_modules/pg-tsquery/index")' has no call signatures.

I think the fix would be something like this in index.d.ts instead of the export default:

declare function tsquery(options?: TsqueryOptions): (str: string) => string;
export = tsquery

For now a work around is this goofy thing:

import { default as Tsquery } from 'pg-tsquery'

type TsQueryFunc = (options?: Tsquery.TsqueryOptions) => (str: string) => string
const tsqueryParser = (Tsquery as unknown as TsQueryFunc)()

The typescript guys built this little tool that seems to agree with my analysis:
https://arethetypeswrong.github.io/?p=pg-tsquery%408.4.1

@caub
Copy link
Owner

caub commented Oct 25, 2023

does it work if you use this?

import { Tsquery } from 'pg-tsquery';

const parser = new Tsquery(/* options can be passed to override the defaults */);

@jefbarn
Copy link
Contributor Author

jefbarn commented Oct 25, 2023

TS2614: Module  'pg-tsquery'  has no exported member  Tsquery . Did you mean to use  import Tsquery from "pg-tsquery"  instead?
The published npm module doesn't have Tsquery class defined.

Tried loading the version from github and got this from node:
import { Tsquery } from 'pg-tsquery';
^^^^^^^
SyntaxError: Named export 'Tsquery' not found. The requested module 'pg-tsquery' is a CommonJS module, which may not support all module.exports as named exports.

The issue is if you use the node16 module format to use native es modules in node it won't work because the typescript definition is trying to say this is an es module but it's actually cjs.

@jefbarn
Copy link
Contributor Author

jefbarn commented Oct 25, 2023

Created a pull request that should match the actual exported code. Take a look.
#26

@caub
Copy link
Owner

caub commented Oct 26, 2023

Thanks, I'm not so familiar with TS, so if this fixes the type issues, I'll merge it, thanks again

@caub caub closed this as completed in #26 Oct 26, 2023
@jschuur
Copy link

jschuur commented Jan 29, 2024

Will you do a new npm release with this fix? The current 8.4.1 release doesn't have this yet.

@caub
Copy link
Owner

caub commented Jan 31, 2024

done sorry

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants