Skip to content

Node modules and typescript types #124

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

Closed
AnthonyMonterrosa opened this issue Nov 17, 2020 · 2 comments
Closed

Node modules and typescript types #124

AnthonyMonterrosa opened this issue Nov 17, 2020 · 2 comments

Comments

@AnthonyMonterrosa
Copy link

I'm trying to use this package with a Typescript setup with node. I'd like to use the Typescript typing information and modern module syntax, i.e. import { ... } from 'postgres' instead of ... = require('postgres'), and I'm running into issues and would like your advice.

Using the module import syntax, I'm able to retrieve all type information for Typescript, as the import seems to pull from the typing file directly:
image
But from here, I can't figure out how to use the library. Intellisense doesn't find the typical postgres(...) function that's demo'd in the README. Trying to use the import as-is results in a Typescript build error:
image
image
image

On the other side, if I use the require syntax, I'm able to use the library as normal, but then I'm not sure how to get the type information:
image
I'm not allowed to use both importing styles together, so I can't do them both to get the library implementation and types.

Please let me know if I'm missing something obvious or using the library incorrectly.

I'm using the 2.0.0-beta.2 release.

node --version: v15.0.1

@Minigugus
Copy link
Contributor

Minigugus commented Nov 17, 2020

@AnthonyMonterrosa TLDR: Add "allowSyntheticDefaultImports": true, in your tsconfig.json, then import PostgreSQL from 'postgres'

postgres has the same problem as many CommonJS packages: it directly expose an augmented function with additionnal properties (postgres.BigInt, etc.), so neither import { ... } from 'postgres', import PostgreSQL from 'postgres' nor import * as PostgreSQL from 'postgres' can work properly. TypeScript designers addressed the issue by adding the allowSyntheticDefaultImports property in tsconfig.json to allow import postgres from 'postgres' to work but no longer complies with the ES module specification (the default import should come from module.exports.default but postgres override directly module.exports).

@porsager This is not mentioned in the README. Is this an oversight? #84 adds TypeScript tips in README, maybe I can add this one too.

@AnthonyMonterrosa
Copy link
Author

That worked! Thank you. I don't think I would have figured that out without your advice.

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

No branches or pull requests

2 participants