TypeScript

Fastify is shipped with a typings file, but it still require to install @types/node, depending on the Node.js version that you are using.

Types support

We do care about the TypeScript community, but the framework is written in plain JavaScript and currently no one of the core team is a TypeScript user while only one of the collaborators is. We do our best to have the typing updated with the latest version of the API, but it can happen that the typings are not in sync.Luckly 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 Plugin Types for more information.

Example

This example TypeScript app closely aligns with the JavaScript examples:

  1. import * as fastify from 'fastify'
  2. import { Server, IncomingMessage, ServerResponse } from 'http'
  3. // Create a http server. We pass the relevant typings for our http version used.
  4. // By passing types we get correctly typed access to the underlying http objects in routes.
  5. // If using http2 we'd pass <http2.Http2Server, http2.Http2ServerRequest, http2.Http2ServerResponse>
  6. const server: fastify.FastifyInstance<Server, IncomingMessage, ServerResponse> = fastify({})
  7. const opts: fastify.RouteShorthandOptions = {
  8. schema: {
  9. response: {
  10. 200: {
  11. type: 'object',
  12. properties: {
  13. pong: {
  14. type: 'string'
  15. }
  16. }
  17. }
  18. }
  19. }
  20. }
  21. server.get('/ping', opts, (request, reply) => {
  22. console.log(reply.res) // this is the http.ServerResponse with correct typings!
  23. reply.code(200).send({ pong: 'it worked!' })
  24. })

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. type: 'object',
  27. properties: {
  28. bar: {
  29. type: 'string'
  30. }
  31. }
  32. },
  33. body: {
  34. type: 'object',
  35. properties: {
  36. baz: {
  37. type: 'string'
  38. }
  39. }
  40. },
  41. headers: {
  42. type: 'object',
  43. properties: {
  44. a: {
  45. type: 'string'
  46. }
  47. }
  48. }
  49. }
  50. }
  51. server.get<Query, Params, Body, Headers>('/ping/:bar', opts, (request, reply) => {
  52. console.log(request.query) // this is of type Query!
  53. console.log(request.params) // this is of type Params!
  54. console.log(request.body) // this is of type Body!
  55. console.log(request.headers) // this is of type Headers!
  56. reply.code(200).send({ pong: 'it worked!' })
  57. })

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

  1. import * as fastify from 'fastify'
  2. const server = fastify({})
  3. interface Params {
  4. bar?: string
  5. }
  6. const opts: fastify.RouteShorthandOptions = {
  7. schema: {
  8. params: {
  9. type: 'object',
  10. properties: {
  11. bar: {
  12. type: 'string'
  13. }
  14. }
  15. },
  16. }
  17. }
  18. server.get<fastify.DefaultQuery, Params, unknown>('/ping/:bar', opts, (request, reply) => {
  19. console.log(request.query) // this is of type fastify.DefaultQuery!
  20. console.log(request.params) // this is of type Params!
  21. console.log(request.body) // this is of type unknown!
  22. console.log(request.headers) // this is of type fastify.DefaultHeader because typescript will use the default type value!
  23. reply.code(200).send({ pong: 'it worked!' })
  24. })
  25. // Given that you haven't validated the querystring, body, or headers, it would be best
  26. // to type those params as 'unknown'. However, it's up to you. The example below is the
  27. // best way to prevent you from shooting yourself in the foot. In other words, don't
  28. // use values you haven't validated.
  29. server.get<unknown, Params, unknown, unknown>('/ping/:bar', opts, (request, reply) => {
  30. console.log(request.query) // this is of type unknown!
  31. console.log(request.params) // this is of type Params!
  32. console.log(request.body) // this is of type unknown!
  33. console.log(request.headers) // this is of type unknown!
  34. reply.code(200).send({ pong: 'it worked!' })
  35. })

HTTP Prototypes

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. server.get('/ping', (request, reply) => {
  7. // Access our custom method on the http prototype
  8. const clientDeviceType = request.raw.getClientDeviceType()
  9. reply.send({ clientDeviceType: `you called this endpoint from a ${clientDeviceType}` })
  10. })

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 pluginsMake sure to read our CONTRIBUTING.md file before getting started to make sure things go smoothly!

Core Types

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

Plugin Types

Plugins maintained by and orginized under the fastify organization on GitHub should ship with typings just like fastify itself does. Some plugins already include typings but many do not. We are happy to accept contributions to those plugins without any typings, see fastify-cors for an example of a plugin that comes with it's own typings.

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 here.

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

Authoring Plugin Types

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.

  1. // require fastify typings
  2. import fastify = require("fastify");
  3. // require necessary http typings
  4. import { Server, IncomingMessage, ServerResponse } from "http";
  5. // extend fastify typings
  6. declare module "fastify" {
  7. interface FastifyReply<HttpResponse> {
  8. sendFile(filename: string): FastifyReply<HttpResponse>;
  9. }
  10. }
  11. // declare plugin type using fastify.Plugin
  12. declare const fastifyStatic: fastify.Plugin<Server, IncomingMessage, ServerResponse, {
  13. root: string;
  14. prefix?: string;
  15. serve?: boolean;
  16. decorateReply?: boolean;
  17. schemaHide?: boolean;
  18. setHeaders?: (...args: any[]) => void;
  19. acceptRanges?: boolean;
  20. cacheControl?: boolean;
  21. dotfiles?: boolean;
  22. etag?: boolean;
  23. extensions?: string[];
  24. immutable?: boolean;
  25. index?: string[];
  26. lastModified?: boolean;
  27. maxAge?: string | number;
  28. }>;
  29. // export plugin type
  30. export = fastifyStatic;

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. reply.sendFile('some-file-name')
  27. })

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