Skip to content

Add TypeScript typings #63

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

Merged
merged 13 commits into from
Sep 2, 2020
Merged

Add TypeScript typings #63

merged 13 commits into from
Sep 2, 2020

Conversation

evgenymarkov
Copy link

@evgenymarkov evgenymarkov commented Aug 22, 2020

Hello! I tried to use this plugin in my project written in TypeScript and it didn't work due to lack of typings. Please check my changes. I can polish them if there are any comments.

Caveats:

Transact helper seems overcomplicated. I couldn't write the types for this case:

fastify.pg.transact((client, commit) => {
  client.query('INSERT INTO users(username) VALUES($1) RETURNING id', [req.params.username], (err, id) => {
    commit(err, id)
  });
});

Also, this is very dangerous code, because if you forget to add catch() to the transaction, you will get an unhandled promise rejection error.

Checklist

Videos from VSCode: database, transaction.

@kibertoad
Copy link
Member

Thanks! Can you also add tsd tests?

@evgenymarkov
Copy link
Author

@kibertoad I tried to write tsd tests, but tsd crashes with named tuples introduced in typescript 4.0. Named tuples are used to describe the Node-style callback function.

Do you know any other ways to describe callbacks like this?

cb(error: null, result: SomeResult);
cb(error: SomeError, result: undefined);

@kibertoad
Copy link
Member

@evgenymarkov Isn't named tuple an overkill here? Why not just do

type CallbackXYZ = (error?: SomeError, result?: SomeResult) => void;

"compilerOptions": {
/* Basic Options */
"noEmit": true,
"lib": ["ES2020"],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this version specifically?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It just example. I downgraded target settings in 4851fbd.

index.d.ts Outdated
transact: typeof transact;
};

type Options = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why aren't Options exported?

Copy link
Author

@evgenymarkov evgenymarkov Aug 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in f2a868e.


set -ex

npx tsc --project ./examples/typescript/single-db/tsconfig.json
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not cross-platform. why not make it an npm script instead?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 9e1ca07

tsconfig.json Outdated
@@ -0,0 +1,14 @@
{
"include": ["./index.d.ts"],

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@evgenymarkov
Copy link
Author

@kibertoad Thank you for review! I will respond or fix the comments asap.

@evgenymarkov evgenymarkov requested a review from kibertoad August 23, 2020 00:42
Copy link
Member

@kibertoad kibertoad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@evgenymarkov
Copy link
Author

@darkgl0w Hi, do you want to review pull request?

@evgenymarkov evgenymarkov requested a review from fox1t August 29, 2020 21:43
Copy link
Member

@fox1t fox1t left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Great work!

@kibertoad kibertoad merged commit b98bee8 into fastify:master Sep 2, 2020
@evgenymarkov evgenymarkov deleted the typings branch September 2, 2020 23:19
@BlackGlory
Copy link

It should be released as v3.0.2, the current version does not have index.d.ts.

mcollina pushed a commit that referenced this pull request Nov 9, 2020
* Types: Add "pg" property to FastifyInstance

As recommended in the guide for creating TypeScript compatible Fastify plugins, properties that the Fastify instances are decorated with should also in the types get added to those instances: https://www.fastify.io/docs/latest/TypeScript/#creating-a-typescript-fastify-plugin

This change follows up on #63 by adding that.

* Document "name" option

* Enable use of non-docker postgres db in tests

* Fix tests when no error is thrown

* Throw if named and unnamed are combined

* Clarify that intention is to manually add type
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 this pull request may close these issues.

6 participants