Skip to content

Commit 6260ef5

Browse files
authored
refactor(types): migrate from tsd to tstyche (#217)
1 parent edd73a3 commit 6260ef5

6 files changed

Lines changed: 42 additions & 50 deletions

File tree

package.json

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"test": "npm run test:unit && npm run test:typescript",
1515
"test:unit": "c8 --100 node --test",
1616
"test:report": "standard && c8 --reporter html node --test",
17-
"test:typescript": "tsd"
17+
"test:typescript": "tstyche"
1818
},
1919
"repository": {
2020
"type": "git",
@@ -74,16 +74,13 @@
7474
"neostandard": "^0.13.0",
7575
"pg": "^8.11.3",
7676
"pg-native": "^3.0.1",
77-
"tsd": "^0.33.0",
77+
"tstyche": "^7.0.0",
7878
"typescript": "~6.0.2"
7979
},
8080
"peerDependencies": {
8181
"pg": ">=6.0.0"
8282
},
83-
"tsd": {
84-
"directory": "test-types"
85-
},
8683
"publishConfig": {
8784
"access": "public"
8885
}
89-
}
86+
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import fastify from 'fastify'
22
import * as pg from 'pg'
3-
import { expectAssignable, expectType } from 'tsd'
4-
5-
import fastifyPostgres, { PostgresDb } from '../index'
3+
import { expect } from 'tstyche'
4+
import fastifyPostgres, { PostgresDb } from '..'
65

76
const app = fastify()
87

@@ -34,6 +33,7 @@ app.register(fastifyPostgres, {
3433

3534
// Plugin property available
3635
app.after(() => {
37-
expectAssignable<PostgresDb>(app.pg)
38-
expectType<PostgresDb>(app.pg.users)
36+
expect(app.pg).type.toBeAssignableTo<PostgresDb | undefined>()
37+
expect(app.pg.users).type.toBe<PostgresDb | undefined>()
38+
expect(app.pg.posts).type.toBe<PostgresDb | undefined>()
3939
})

test-types/query.test-d.ts

Lines changed: 0 additions & 30 deletions
This file was deleted.

test-types/query.tst.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import fastify from 'fastify'
2+
import { type Client, type Pool, type PoolClient, type QueryResult } from 'pg'
3+
import { expect } from 'tstyche'
4+
import fastifyPostgres, { type PostgresDb } from '..'
5+
6+
const app = fastify()
7+
8+
app.register(fastifyPostgres, {
9+
connectionString: 'postgres://user:password@host:port/db',
10+
})
11+
12+
app.get('/calc', async () => {
13+
expect(app.pg).type.toBeAssignableTo<PostgresDb>()
14+
15+
expect(app.pg.pool).type.toBe<Pool>()
16+
expect(app.pg.Client).type.toBe<Client>()
17+
18+
const client = await app.pg.connect()
19+
expect(client).type.toBe<PoolClient>()
20+
21+
const sumResult = await client.query<{ sum: number }>('SELECT 2 + 2 as sum')
22+
expect(sumResult).type.toBe<QueryResult<{ sum: number }>>()
23+
24+
client.release()
25+
26+
return { sum: sumResult.rows }
27+
})
Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import fastify from 'fastify'
22
import { PoolClient, QueryResult } from 'pg'
3-
import { expectType } from 'tsd'
4-
5-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
6-
import fastifyPostgres, { PostgresDb } from '../index'
3+
import { expect } from 'tstyche'
4+
import fastifyPostgres from '..'
75

86
const app = fastify()
97

@@ -19,12 +17,12 @@ app.post('/insert-async', async () => {
1917
`
2018

2119
const transactionResult = await app.pg.transact((client) => {
22-
expectType<PoolClient>(client)
20+
expect(client).type.toBe<PoolClient>()
2321

2422
return client.query<{ sum: number }>(insertQuery)
2523
})
2624

27-
expectType<QueryResult<{ sum: number }>>(transactionResult)
25+
expect(transactionResult).type.toBe<QueryResult<{ sum: number }>>()
2826

2927
return transactionResult
3028
})
@@ -38,13 +36,13 @@ app.post('/insert-cb', (_req, reply) => {
3836

3937
app.pg.transact(
4038
(client) => {
41-
expectType<PoolClient>(client)
39+
expect(client).type.toBe<PoolClient>()
4240

4341
return client.query<{ sum: number }>(insertQuery)
4442
},
4543
(error, result) => {
46-
expectType<Error | null>(error)
47-
expectType<QueryResult<{ sum: number }> | undefined>(result)
44+
expect(error).type.toBe<Error | null>()
45+
expect(result).type.toBe<QueryResult<{ sum: number }> | undefined>()
4846

4947
if (error) {
5048
reply.status(500).send(error)

0 commit comments

Comments
 (0)