State of TypeScript and Fastify #2877
Replies: 5 comments 16 replies
-
Hi,
The biggest pain point for me is generics and hooks, such things like support in hooks arrays of functions, or just functions which may use Example{
preValidation: ((
request: FastifyRequest<{ Querystring: { fileName: string | string[] } }>, _reply: FastifyReply, done: () => void,
) => {
if (!Array.isArray(request.query.fileName)) request.query.fileName = [request.query.fileName];
done();
}) as preValidationHookHandler
} without casting it as `preValidationHookHandler explicitely I'm facing with error
I've written own library to deal with it, so my solution is to use In general my TS code looks like several controllers, services, entities, I also use separated files for schemas and their TS types :) Exampleschemas.ts: import { Type } from '@sinclair/typebox'
export const filesSchema = Type.Object({
fileName: Type.Array(Type.String()),
}, {
examples: [
{
name: 'Single file',
summary: 'Single file',
value: { fileName: 'sample.xd' },
},
{
name: 'Multiple files',
summary: 'Multiple files',
value: { fileName: ['sample.xd', 'sample2.xd'] },
},
],
})
export type Files = Static<typeof filesSchema> FileController.ts: import { Controller, GET } from 'fastify-decorators'
import { filesSchema, Files } from './schemas.js'
@Controller('/files')
export class FilesController {
@GET('/', { schema: { query: filesSchema } })
async getFiles(request: FastifyRequest<{ Querystring: Files }>) {
// async stuff here
}
} |
Beta Was this translation helpful? Give feedback.
-
A big pain point for me is that you need to use module augmentation to add types for the decorators that you add. I totally understand why it is necessary, but it is arcane syntax that I would guess many aren't familiar with, and requires a pretty deep understanding of the TS module concepts to get right. Another one is that the types aren't aware of the encapsulation context, so you can't quite trust the types of the decorators anyways. If you decorate within an encapsulation context, you either have to type all Having a blessed path for maintaining JSON schemas and derived types without writing them out twice would be dandy as well. |
Beta Was this translation helpful? Give feedback.
-
Having an out-of-the-box system to translate json schemas to types would be awesome. |
Beta Was this translation helpful? Give feedback.
-
Check out this encapsulation solution I came up with: type Server = {
decorate: (key: string, value: any) => void
get: (url: string, handler: (request: unknown, response: unknown) => void) => void
}
type Options = unknown
type FPlugin<S = unknown> = (server: Server & S, options: Options) => Promise<void>
type EncapsulatedFPlugin<P extends FPlugin<any>> = FPlugin<Parameters<P>[0]>
type AuthDecoration = {
foo: string
}
const authPlugin: FPlugin<AuthDecoration> = async (server, options) => {
server.decorate('foo', 'bar')
}
const privateRoute: EncapsulatedFPlugin<typeof authPlugin> = async (server, options) => {
server.get('/', (request, response) => {
server.foo
})
} This could be potentially useful for properly encapsulated fastify plugin types without the need of TypeScript decorator syntax |
Beta Was this translation helpful? Give feedback.
-
Hi Fastify team, love the work you're doing! I just have a suggestion based on my experience using documentation for other projects - I'm wondering if it would be possible to integrate TS info into the each of the docs rather than having it as its own separate page (e.g. including info on how to type plugins within the plugins documentation)? Also as a bonus suggestion - I've never worked with metalsmith so I'm not sure how hard it would be to add this functionality, but having some sort of code toggle between JS and TS examples would be amazing :) The example below is from the Redux Toolkit docs. |
Beta Was this translation helpful? Give feedback.
-
Hi folks! I wanted to open a discussion about the state of TypeScript in fastify. We've had many new contributes join us helping improve TypeScript support across the ecosystem. Here are some questions to get us started.
Furthermore, if there is something else you'd like to see discussed feel free to ask the question! Everyone (users & contributors) are encourage to reply (yes, even the folks who don't use TypeScript but want better support for things like code-hints in their editor) ❤️
Beta Was this translation helpful? Give feedback.
All reactions