Skip to content

fix: ReplyTypeConstrainer array type inference#4885

Merged
mcollina merged 3 commits intofastify:mainfrom
pedroescumalha:fix-reply-type
Jul 26, 2023
Merged

fix: ReplyTypeConstrainer array type inference#4885
mcollina merged 3 commits intofastify:mainfrom
pedroescumalha:fix-reply-type

Conversation

@pedroescumalha
Copy link
Contributor

@pedroescumalha pedroescumalha commented Jul 7, 2023

Closes #4883

As @aadito123 mentioned on the issue, there's a pitfall here: if someone wants the Reply type to be an object with keys as http codes like { 401: string, 409: string }, the inference of the return type will be string. A possible workaround for this case would be { '4xx': { 401: string, 409: string } }.

Checklist

@github-actions github-actions bot added the typescript TypeScript related label Jul 7, 2023
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@Fdawgs Fdawgs requested a review from a team July 8, 2023 08:09
Make sure the reply type is a valid object with only http codes as keys
@pedroescumalha
Copy link
Contributor Author

Hey @mcollina could you take a look again? Found an issue with the previous type definition:

import fastify from '../fastify'

const server = fastify()

type TestReply = {
    200: string;
    10000: Date;
};

server.get<{ Reply: TestReply }>(
  '/ping',
  { schema: {} },
  async (request, reply) => {
    reply.code(10000).send() // type is inferred to a Date
  }
)

The type has an invalid status code, and code().send() was still inferring to TestResponse[code].
In this new implementation we make sure that if the reply object consists of only valid http status codes we infer to TestReply[code], otherwise it's infered to TestReply.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@Uzlopak
Copy link
Contributor

Uzlopak commented Jul 8, 2023

I have to be honest:
This PR is quiete complicated. The typings test imply that the underlying issue is fixed. But does it have other effects? I dont know.
Unfortunately the typings tests are actually not good enough, as we actually needed to determine what the first Parameter of .send exactly is. We currently implicitly check that. This could mean that .send() parameter is implicitly any or so, and that is why maybe other cases, which were before stricter, are now less strict and thus dont throw error in tsd.

I personally dont know, if this PR means regressions in other cases.

@mcollina mcollina requested review from climba03003 and fox1t July 9, 2023 14:04
@mcollina
Copy link
Member

mcollina commented Jul 9, 2023

@climba03003 @fox1t wdyt?

types/reply.d.ts Outdated

type ReplyTypeConstrainer<RouteGenericReply, Code extends ReplyKeysToCodes<keyof RouteGenericReply>> =
RouteGenericReply extends HttpCodesReplyType & Record<Exclude<keyof RouteGenericReply, keyof HttpCodesReplyType>, never> ?
Code extends keyof RouteGenericReply ? Required<RouteGenericReply[Code]> :
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if Required is needed.
It is actually changing the original generic to all property must exist, doesn't it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, it's not needed at all. Just removed it.

@mcollina mcollina merged commit 9d2da75 into fastify:main Jul 26, 2023
renovate bot referenced this pull request in tomacheese/telcheck Jul 27, 2023
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [fastify](https://www.fastify.io/)
([source](https://togithub.com/fastify/fastify)) | [`4.20.0` ->
`4.21.0`](https://renovatebot.com/diffs/npm/fastify/4.20.0/4.21.0) |
[![age](https://developer.mend.io/api/mc/badges/age/npm/fastify/4.21.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/fastify/4.21.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/fastify/4.20.0/4.21.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/fastify/4.20.0/4.21.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>fastify/fastify (fastify)</summary>

###
[`v4.21.0`](https://togithub.com/fastify/fastify/releases/tag/v4.21.0)

[Compare
Source](https://togithub.com/fastify/fastify/compare/v4.20.0...v4.21.0)

#### What's Changed

- chore: remove license-checker package by
[@&#8203;Uzlopak](https://togithub.com/Uzlopak) in
[https://github.com/fastify/fastify/pull/4914](https://togithub.com/fastify/fastify/pull/4914)
- chore: remove pump devDependency by
[@&#8203;Uzlopak](https://togithub.com/Uzlopak) in
[https://github.com/fastify/fastify/pull/4913](https://togithub.com/fastify/fastify/pull/4913)
- ci: create artifacts in coverage workflows by
[@&#8203;Uzlopak](https://togithub.com/Uzlopak) in
[https://github.com/fastify/fastify/pull/4909](https://togithub.com/fastify/fastify/pull/4909)
- docs(server): grammar and structure fixes by
[@&#8203;Fdawgs](https://togithub.com/Fdawgs) in
[https://github.com/fastify/fastify/pull/4904](https://togithub.com/fastify/fastify/pull/4904)
- docs: Fix typo in TypeScript docs
("[@&#8203;types/node](https://togithub.com/types/node)", not
"[@&#8203;node/types](https://togithub.com/node/types)") by
[@&#8203;jasongwartz](https://togithub.com/jasongwartz) in
[https://github.com/fastify/fastify/pull/4922](https://togithub.com/fastify/fastify/pull/4922)
- \[readme] add CII Best Practices Badge by
[@&#8203;ljharb](https://togithub.com/ljharb) in
[https://github.com/fastify/fastify/pull/4926](https://togithub.com/fastify/fastify/pull/4926)
- fix: lowercase type-providers headers types by
[@&#8203;toomuchdesign](https://togithub.com/toomuchdesign) in
[https://github.com/fastify/fastify/pull/4928](https://togithub.com/fastify/fastify/pull/4928)
- ERR_REP_ALREADY_SENT hint that a route may be missing "return reply"
by [@&#8203;mcollina](https://togithub.com/mcollina) in
[https://github.com/fastify/fastify/pull/4921](https://togithub.com/fastify/fastify/pull/4921)
- fix: ReplyTypeConstrainer array type inference by
[@&#8203;pedroescumalha](https://togithub.com/pedroescumalha) in
[https://github.com/fastify/fastify/pull/4885](https://togithub.com/fastify/fastify/pull/4885)

#### New Contributors

- [@&#8203;jasongwartz](https://togithub.com/jasongwartz) made their
first contribution in
[https://github.com/fastify/fastify/pull/4922](https://togithub.com/fastify/fastify/pull/4922)
- [@&#8203;ljharb](https://togithub.com/ljharb) made their first
contribution in
[https://github.com/fastify/fastify/pull/4926](https://togithub.com/fastify/fastify/pull/4926)
- [@&#8203;toomuchdesign](https://togithub.com/toomuchdesign) made their
first contribution in
[https://github.com/fastify/fastify/pull/4928](https://togithub.com/fastify/fastify/pull/4928)
- [@&#8203;pedroescumalha](https://togithub.com/pedroescumalha) made
their first contribution in
[https://github.com/fastify/fastify/pull/4885](https://togithub.com/fastify/fastify/pull/4885)

**Full Changelog**:
fastify/fastify@v4.20.0...v4.21.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/tomacheese/telcheck).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNi4yNC4yIiwidXBkYXRlZEluVmVyIjoiMzYuMjQuMiIsInRhcmdldEJyYW5jaCI6Im1hc3RlciJ9-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
renovate bot referenced this pull request in redwoodjs/graphql Aug 2, 2023
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [fastify](https://www.fastify.io/)
([source](https://togithub.com/fastify/fastify)) | [`4.20.0` ->
`4.21.0`](https://renovatebot.com/diffs/npm/fastify/4.20.0/4.21.0) |
[![age](https://developer.mend.io/api/mc/badges/age/npm/fastify/4.21.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/fastify/4.21.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/fastify/4.20.0/4.21.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/fastify/4.20.0/4.21.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>fastify/fastify (fastify)</summary>

###
[`v4.21.0`](https://togithub.com/fastify/fastify/releases/tag/v4.21.0)

[Compare
Source](https://togithub.com/fastify/fastify/compare/v4.20.0...v4.21.0)

#### What's Changed

- chore: remove license-checker package by
[@&#8203;Uzlopak](https://togithub.com/Uzlopak) in
[https://github.com/fastify/fastify/pull/4914](https://togithub.com/fastify/fastify/pull/4914)
- chore: remove pump devDependency by
[@&#8203;Uzlopak](https://togithub.com/Uzlopak) in
[https://github.com/fastify/fastify/pull/4913](https://togithub.com/fastify/fastify/pull/4913)
- ci: create artifacts in coverage workflows by
[@&#8203;Uzlopak](https://togithub.com/Uzlopak) in
[https://github.com/fastify/fastify/pull/4909](https://togithub.com/fastify/fastify/pull/4909)
- docs(server): grammar and structure fixes by
[@&#8203;Fdawgs](https://togithub.com/Fdawgs) in
[https://github.com/fastify/fastify/pull/4904](https://togithub.com/fastify/fastify/pull/4904)
- docs: Fix typo in TypeScript docs
("[@&#8203;types/node](https://togithub.com/types/node)", not
"[@&#8203;node/types](https://togithub.com/node/types)") by
[@&#8203;jasongwartz](https://togithub.com/jasongwartz) in
[https://github.com/fastify/fastify/pull/4922](https://togithub.com/fastify/fastify/pull/4922)
- \[readme] add CII Best Practices Badge by
[@&#8203;ljharb](https://togithub.com/ljharb) in
[https://github.com/fastify/fastify/pull/4926](https://togithub.com/fastify/fastify/pull/4926)
- fix: lowercase type-providers headers types by
[@&#8203;toomuchdesign](https://togithub.com/toomuchdesign) in
[https://github.com/fastify/fastify/pull/4928](https://togithub.com/fastify/fastify/pull/4928)
- ERR_REP_ALREADY_SENT hint that a route may be missing "return reply"
by [@&#8203;mcollina](https://togithub.com/mcollina) in
[https://github.com/fastify/fastify/pull/4921](https://togithub.com/fastify/fastify/pull/4921)
- fix: ReplyTypeConstrainer array type inference by
[@&#8203;pedroescumalha](https://togithub.com/pedroescumalha) in
[https://github.com/fastify/fastify/pull/4885](https://togithub.com/fastify/fastify/pull/4885)

#### New Contributors

- [@&#8203;jasongwartz](https://togithub.com/jasongwartz) made their
first contribution in
[https://github.com/fastify/fastify/pull/4922](https://togithub.com/fastify/fastify/pull/4922)
- [@&#8203;ljharb](https://togithub.com/ljharb) made their first
contribution in
[https://github.com/fastify/fastify/pull/4926](https://togithub.com/fastify/fastify/pull/4926)
- [@&#8203;toomuchdesign](https://togithub.com/toomuchdesign) made their
first contribution in
[https://github.com/fastify/fastify/pull/4928](https://togithub.com/fastify/fastify/pull/4928)
- [@&#8203;pedroescumalha](https://togithub.com/pedroescumalha) made
their first contribution in
[https://github.com/fastify/fastify/pull/4885](https://togithub.com/fastify/fastify/pull/4885)

**Full Changelog**:
fastify/fastify@v4.20.0...v4.21.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/redwoodjs/redwood).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNi4yNy4xIiwidXBkYXRlZEluVmVyIjoiMzYuMjcuMSIsInRhcmdldEJyYW5jaCI6Im1haW4ifQ==-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 26, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

typescript TypeScript related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

reply.code().send() does not infer correct type

5 participants