TypeScript
TypeScript
Fastify is shipped with a typings file, but you may need to install @types/node
, 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 Plugin Types for more information.
Example
This example TypeScript app closely aligns with the JavaScript examples:
import * as fastify from 'fastify'
import { Server, IncomingMessage, ServerResponse } from 'http'
// Create a http server. We pass the relevant typings for our http version used.
// By passing types we get correctly typed access to the underlying http objects in routes.
// If using http2 we'd pass <http2.Http2Server, http2.Http2ServerRequest, http2.Http2ServerResponse>
// For https pass http2.Http2SecureServer or http.SecureServer instead of Server.
const server: fastify.FastifyInstance<Server, IncomingMessage, ServerResponse> = fastify({})
const opts: fastify.RouteShorthandOptions = {
schema: {
response: {
200: {
type: 'object',
properties: {
pong: {
type: 'string'
}
}
}
}
}
}
server.get('/ping', opts, (request, reply) => {
console.log(reply.res) // this is the http.ServerResponse with correct typings!
reply.code(200).send({ pong: 'it worked!' })
})
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:
import * as fastify from 'fastify'
const server = fastify({})
interface Query {
foo?: number
}
interface Params {
bar?: string
}
interface Body {
baz?: string
}
interface Headers {
a?: string
}
const opts: fastify.RouteShorthandOptions = {
schema: {
querystring: {
type: 'object',
properties: {
foo: {
type: 'number'
}
}
},
params: {
type: 'object',
properties: {
bar: {
type: 'string'
}
}
},
body: {
type: 'object',
properties: {
baz: {
type: 'string'
}
}
},
headers: {
type: 'object',
properties: {
a: {
type: 'string'
}
}
}
}
}
server.get<Query, Params, Headers, Body>('/ping/:bar', opts, (request, reply) => {
console.log(request.query) // this is of type Query!
console.log(request.params) // this is of type Params!
console.log(request.headers) // this is of type Headers!
console.log(request.body) // this is of type Body!
reply.code(200).send({ pong: 'it worked!' })
})
All generic types are optional, so you can also pass types for the parts you validate with schemas:
import * as fastify from 'fastify'
const server = fastify({})
interface Params {
bar?: string
}
const opts: fastify.RouteShorthandOptions = {
schema: {
params: {
type: 'object',
properties: {
bar: {
type: 'string'
}
}
},
}
}
server.get<fastify.DefaultQuery, Params, unknown>('/ping/:bar', opts, (request, reply) => {
console.log(request.query) // this is of type fastify.DefaultQuery!
console.log(request.params) // this is of type Params!
console.log(request.headers) // this is of type unknown!
console.log(request.body) // this is of type fastify.DefaultBody because typescript will use the default type value!
reply.code(200).send({ pong: 'it worked!' })
})
// Given that you haven't validated the querystring, body, or headers, it would be best
// to type those params as 'unknown'. However, it's up to you. The example below is the
// best way to prevent you from shooting yourself in the foot. In other words, don't
// use values you haven't validated.
server.get<unknown, Params, unknown, unknown>('/ping/:bar', opts, (request, reply) => {
console.log(request.query) // this is of type unknown!
console.log(request.params) // this is of type Params!
console.log(request.headers) // this is of type unknown!
console.log(request.body) // this is of type unknown!
reply.code(200).send({ pong: 'it worked!' })
})