Skip to content

chore: introduce TSTyche for type testing#6532

Merged
Eomm merged 13 commits into
fastify:mainfrom
mrazauskas:introduce-tstyche
May 17, 2026
Merged

chore: introduce TSTyche for type testing#6532
Eomm merged 13 commits into
fastify:mainfrom
mrazauskas:introduce-tstyche

Conversation

@mrazauskas

@mrazauskas mrazauskas commented Feb 25, 2026

Copy link
Copy Markdown
Contributor

Context #6033

This PR is introducing TSTyche for testing TypeScript types.

I have a branch that is replacing tsd with tstyche here #6525. To be honest, I wanted to look through it once again, but that is too massive to review.

This is clean up PR that allows to run the same type tests files using tsd and tstyche.

Changes

  • expectError() is replaced with expectNotAssignable() or // @ts-expect-error

    • mostly when tsds expectError() is provided a type argument (like expectError<string>("abc")), it catches assignability error and therefore it is replaced with expectNotAssignable() for better readability
    • otherwise it is replaced with // @ts-expect-error followed by an error message, these are checked (and required!) by TSTyche
  • expectNotDeprecated() is removed. It does nothing. All exported types and expressions would pass this test.

Other Changes

  • test/types/import.ts, it was testing that FastifyListenOptions and FastifyLogFn are exported. I aded tests for FastifyListenOptions to test/types/instance.test-d.ts and FastifyLogFn is already tested in test/types/logger.test-d.ts and test/types/request.test-d.ts
  • the test:typescript script is renamed to test:types
  • ESLint config is updated to unused constructs in type test files

Checklist

@github-actions github-actions Bot added typescript TypeScript related github actions Github actions related labels Feb 25, 2026
Comment on lines +46 to +47
expectAssignable<FastifyInstance>(fastify().register(import('./dummy-plugin.mjs')))
expectAssignable<FastifyInstance>(fastify().register(import('./dummy-plugin.mjs'), { foo: 1 }))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

After tsconfig.json was added, TypeScript requires ESM file to be imported.

Comment on lines -198 to +203
expectAssignable<ServerWithTypeProviderAndLogger>(
// @ts-expect-error
serverWithTypeProviderAndLogger.register(testPluginWithTypeProviderAndLogger)
)
// @ts-expect-error No overload matches this call.
serverWithTypeProviderAndLogger.register(testPluginWithTypeProviderAndLogger)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Same as other // @ts-expect-error tests above. expectAssignable() seems to be redundant.

expectType<((...args: [payload: string]) => typeof res)>(res.code(200).send)
expectType<((...args: [payload: number]) => typeof res)>(res.code(400).send)
expectType<((...args: [payload: { error: string }]) => typeof res)>(res.code(500).send)
expectError<(payload?: unknown) => typeof res>(res.code(200).send)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The exact types was tested few lines above.

expectType<((...args: [payload: string]) => typeof res)>(res.code(200).send)
expectType<((...args: [payload: number]) => typeof res)>(res.code(400).send)
expectType<((...args: [payload: { [x: string]: unknown; error?: string }]) => typeof res)>(res.code(500).send)
expectError<(payload?: unknown) => typeof res>(res.code(200).send)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The exact types was tested few lines above.

@mrazauskas mrazauskas mentioned this pull request Feb 25, 2026
2 tasks
Comment thread eslint.config.js
Comment on lines +37 to +38
'@typescript-eslint/no-unused-expressions': 'off',
'@typescript-eslint/no-unused-vars': 'off'

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

These are added only temporary. I plan to remove them after migrating the assertions. For instance, introducing .toHaveProperty() in three places allows enabling no-unused-expressions.

// preClose hook types should be exported correctly https://github.com/fastify/fastify/pull/5335
/* eslint-disable @typescript-eslint/no-unused-vars */
preCloseAsyncHookHandler,
preCloseHookHandler

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I included tests for both preClose* types, so they are not reported as unused vars by ESLint anymore.

Eomm
Eomm previously requested changes Mar 7, 2026

@Eomm Eomm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why do we need it?

@mrazauskas

Copy link
Copy Markdown
Contributor Author

Why do we need it?

@Eomm Not sure I fully understand your questions. See the discussion in #6033. Is the approach somehow unclear? Or which part? Happy to provide more details, but at the moment it is not clear what is missing.

@Eomm Eomm linked an issue Mar 7, 2026 that may be closed by this pull request
2 tasks

@Eomm Eomm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Pinged the TS team

LGTM - let's remove tsd after this PR

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm

@mrazauskas mrazauskas requested a review from Eomm April 5, 2026 17:49
@Eomm Eomm dismissed their stale review April 10, 2026 10:56

Not a ts user

@Eomm Eomm requested a review from a team April 10, 2026 10:56
Comment thread test/types/content-type-parser.test-d.ts
@ilteoood ilteoood self-requested a review April 11, 2026 08:04
Signed-off-by: mrazauskas <[email protected]>
@Tony133

Tony133 commented May 13, 2026

Copy link
Copy Markdown
Member

I wanted to let you know that all Fastify libraries and plugins have been migrated from tsd to tstyche for type checking 🙌 🎉 🚀

I think we can merge this PR so that we can proceed with the complete removal of tsd from this repository as well, since there is also this PR: #6525 🔥

The only PR left to merge is this one regarding the Fastify CLI: fastify/fastify-cli#886 🔥

@Tony133 Tony133 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

Puppo added a commit to Puppo/fastify-constraints that referenced this pull request May 14, 2026
- Replace tsd with tstyche for type testing
- Update test:types script to use tstyche
- Convert initialization.test-d.ts to initialization.tst.ts
- Update index.d.ts to use ES module export syntax

PR of Reference:
  - fastify/fastify#6532
  - fastify/fastify#6525
@Eomm Eomm merged commit 436e1f0 into fastify:main May 17, 2026
32 checks passed
@mrazauskas mrazauskas deleted the introduce-tstyche branch May 17, 2026 12:43
Puppo added a commit to Puppo/fastify-constraints that referenced this pull request May 18, 2026
- Replace tsd with tstyche for type testing
- Update test:types script to use tstyche
- Convert initialization.test-d.ts to initialization.tst.ts
- Update index.d.ts to use ES module export syntax

PR of Reference:
  - fastify/fastify#6532
  - fastify/fastify#6525
Puppo added a commit to Puppo/fastify-constraints that referenced this pull request May 18, 2026
- Replace tsd with tstyche for type testing
- Update test:types script to use tstyche
- Convert initialization.test-d.ts to initialization.tst.ts
- Update index.d.ts to use ES module export syntax

PR of Reference:
  - fastify/fastify#6532
  - fastify/fastify#6525
simoneb pushed a commit to nearform/fastify-constraints that referenced this pull request May 18, 2026
* refactor(types): migrate from tsd to tstyche

- Replace tsd with tstyche for type testing
- Update test:types script to use tstyche
- Convert initialization.test-d.ts to initialization.tst.ts
- Update index.d.ts to use ES module export syntax

PR of Reference:
  - fastify/fastify#6532
  - fastify/fastify#6525

* refactor(types): add toBeCallableWith assertion for app.register

* refactor(types): add route-level constraints assertion

* refactor(types): add child.get constraints assertion

* Apply suggestions from code review

Co-authored-by: Tom Mrazauskas <[email protected]>

* Apply suggestions from code review

Co-authored-by: Tom Mrazauskas <[email protected]>

* Update test/types/initialization.tst.ts

* fix(scripts): update test command to run all test files

* refactor(types): update type definitions and test initialization

---------

Co-authored-by: Tom Mrazauskas <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

github actions Github actions related typescript TypeScript related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Should we move out from tsd?

6 participants