Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Type narrowing on status().send() chains #4761

Closed
wants to merge 11 commits into from
37 changes: 32 additions & 5 deletions test/types/type-provider.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import fastify, {
FastifyReply,
FastifyInstance
} from '../../fastify'
import { expectAssignable, expectError, expectType } from 'tsd'
import { expectAssignable, expectError, expectNotAssignable, expectType } from 'tsd'
import { IncomingHttpHeaders } from 'http'
import { Type, TSchema, Static } from '@sinclair/typebox'
import { FromSchema, JSONSchema } from 'json-schema-to-ts'
Expand Down Expand Up @@ -78,13 +78,22 @@ expectAssignable(server.withTypeProvider<TypeBoxProvider>().get(
x: Type.Number(),
y: Type.Number(),
z: Type.Number()
})
}),
response: {
200: Type.String(),
'3xx': Type.Literal('123')
}
}
},
(req) => {
(req, res) => {
expectType<number>(req.body.x)
expectType<number>(req.body.y)
expectType<number>(req.body.z)

// response type narrowing
expectAssignable<(payload: string) => unknown>(res.code(200).send)
expectAssignable<(payload:'abc') => unknown>(res.code(200).send)
expectNotAssignable<(payload: string) => unknown>(res.code(301).send)
}
))

Expand All @@ -107,13 +116,22 @@ expectAssignable(server.withTypeProvider<JsonSchemaToTsProvider>().get(
y: { type: 'string' },
z: { type: 'boolean' }
}
} as const,
response: {
200: { type: 'string' },
'3xx': { const: '123' }
} as const
}
},
(req) => {
(req, res) => {
expectType<number | undefined>(req.body.x)
expectType<string | undefined>(req.body.y)
expectType<boolean | undefined>(req.body.z)

// response type narrowing
expectAssignable<(payload: string) => unknown>(res.code(200).send)
expectAssignable<(payload:'abc') => unknown>(res.code(200).send)
expectNotAssignable<(payload: string) => unknown>(res.code(301).send)
}
))

Expand All @@ -134,13 +152,22 @@ expectAssignable(server.withTypeProvider<TypeBoxProvider>().withTypeProvider<Jso
y: { type: 'string' },
z: { type: 'boolean' }
}
} as const,
response: {
200: { type: 'string' },
'3xx': { const: '123' }
} as const
}
},
(req) => {
(req, res) => {
expectType<number | undefined>(req.body.x)
expectType<string | undefined>(req.body.y)
expectType<boolean | undefined>(req.body.z)

// response type narrowing
expectAssignable<(payload: string) => unknown>(res.code(200).send)
expectAssignable<(payload:'abc') => unknown>(res.code(200).send)
expectNotAssignable<(payload: string) => unknown>(res.code(301).send)
climba03003 marked this conversation as resolved.
Show resolved Hide resolved
}
))

Expand Down