Fastify is shipped with a typings file, but you may need to install , depending on the Node.js version you are using.

Types support

We do care about the TypeScript community, and one of our core team members is currently reworking all types. We do our best to have the typings updated with the latest version of the API, but it can happen that the typings are not in sync.Luckily this is Open Source and you can contribute to fix them, we will be very happy to accept the fix and release it as soon as possible as a patch release. Checkout the contributing rules!

Plugins may or may not include typings. See for more information.

This example TypeScript app closely aligns with the JavaScript examples:

Generic Parameters

Since you can validate the querystring, params, body, and headers, you can also override the default types of those values on the request interface:

  1. import * as fastify from 'fastify'
  2. const server = fastify({})
  3. interface Query {
  4. foo?: number
  5. }
  6. interface Params {
  7. bar?: string
  8. }
  9. interface Body {
  10. baz?: string
  11. }
  12. interface Headers {
  13. a?: string
  14. }
  15. const opts: fastify.RouteShorthandOptions = {
  16. schema: {
  17. querystring: {
  18. type: 'object',
  19. properties: {
  20. foo: {
  21. type: 'number'
  22. }
  23. }
  24. },
  25. params: {
  26. properties: {
  27. bar: {
  28. type: 'string'
  29. }
  30. }
  31. body: {
  32. type: 'object',
  33. properties: {
  34. baz: {
  35. type: 'string'
  36. }
  37. }
  38. },
  39. headers: {
  40. type: 'object',
  41. properties: {
  42. a: {
  43. type: 'string'
  44. }
  45. }
  46. }
  47. }
  48. }
  49. server.get<Query, Params, Headers, Body>('/ping/:bar', opts, (request, reply) => {
  50. console.log(request.query) // this is of type Query!
  51. console.log(request.params) // this is of type Params!
  52. console.log(request.body) // this is of type Body!
  53. console.log(request.headers) // this is of type Headers!
  54. reply.code(200).send({ pong: 'it worked!' })
  55. })

All generic types are optional, so you can also pass types for the parts you validate with schemas:

By default, fastify will determine which version of http is being used based on the options you pass to it. If for any reason you need to override this you can do so as shown below:

  1. interface CustomIncomingMessage extends http.IncomingMessage {
  2. getClientDeviceType: () => string
  3. }
  4. // Passing overrides for the http prototypes to fastify
  5. const server: fastify.FastifyInstance<http.Server, CustomIncomingMessage, http.ServerResponse> = fastify()
  6. // Access our custom method on the http prototype
  7. const clientDeviceType = request.raw.getClientDeviceType()
  8. })

In this example we pass a modified http.IncomingMessage interface since it has been extended elsewhere in our application.

Contributing

TypeScript related changes can be considered to fall into one of two categories:

  • Core - The typings bundled with fastify
  • Plugins - Fastify ecosystem plugins

Make sure to read our CONTRIBUTING.md file before getting started to make sure things go smoothly!

When updating core types you should make a PR to this repository. Ensure you:

  • Update examples/typescript-server.ts to reflect the changes (if necessary)
  • Update test/types/index.ts to validate changes work as expected

Typings for third-party-plugins may either be included with the plugin or hosted on DefinitelyTyped. Remember, if you author a plugin to either include typings or publish them on DefinitelyTyped! Information of how to install typings from DefinitelyTyped can be found .

Some types might not be available yet, so don't be shy about contributing.

Typings for many plugins that extend the FastifyRequest, FastifyReply or FastifyInstance objects can be achieved as shown below.

This code shows the typings for the fastify-static plugin.

Now you are good to go and could use the plugin like so:

  1. import * as Fastify from 'fastify'
  2. import * as fastifyStatic from 'fastify-static'
  3. const app = Fastify()
  4. // the options here are type-checked
  5. app.register(fastifyStatic, {
  6. acceptRanges: true,
  7. cacheControl: true,
  8. decorateReply: true,
  9. dotfiles: true,
  10. etag: true,
  11. extensions: ['.js'],
  12. immutable: true,
  13. index: ['1'],
  14. lastModified: true,
  15. maxAge: '',
  16. prefix: '',
  17. root: '',
  18. schemaHide: true,
  19. serve: true,
  20. setHeaders: (res, pathName) => {
  21. res.setHeader('some-header', pathName)
  22. }
  23. })
  24. app.get('/file', (request, reply) => {
  25. // using newly defined function on FastifyReply
  26. })

Adding typings to all our plugins is a community effort so feel free to contribute!