diff --git a/examples/types/middleware.ts b/examples/types/middleware.ts index 2ad26b4..63538cb 100644 --- a/examples/types/middleware.ts +++ b/examples/types/middleware.ts @@ -5,6 +5,10 @@ type UserRequest = { user: string } & IRequestStrict +type PetRequest = { + pet: string +} & IRequestStrict + // we define a *strict* router for this demo const router = IttyRouter() @@ -13,6 +17,11 @@ const withUser: RequestHandler = (request) => { request.user = 'Kevin' } +// middleware with explicit request-type (generic) +const withPet = (request: PetRequest) => { + request.pet = 'Halsey' +} + router // request will be IRequestStrict here, thus no user property .get('/', (request) => { @@ -20,8 +29,9 @@ router }) // then we add the middleware defined above, allowing the handler chain to inherit the request type - .all('*', withUser, (request) => { - request.user = 'Kevin' + .all('*', withUser, withPet, (request) => { + request.user = 'Kevin' // should work + request.pet = 'Halsey' // should work }) // request will be back to IRequestStrict here, thus no user property