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(types): Type z.request responses; deprecate .json #895

Merged
merged 2 commits into from
Oct 21, 2024

Conversation

tkcranny
Copy link
Collaborator

This adds generic parameters to the HttpResponse and RawHttpResponse types that allow them to specify what they're returning. If not provided the default is any, similar to the previous behaviour.

interface User {
  id: numner,
  name: string,
}

const resp = await z.request<User>('https://example.com/users');
resp.data.firstName // Error: Type `firstName` doesn't exist on type User, did you mean `name`.

Because of this note in the CLI docs, I've also marked the .json attribute on HttpResponse objects as @deprecated. It will now show up in the editor struck out, and suggest using .data instead. Is this deprecating .json correct in your opinion?

Screen Shot 2024-10-18 at 17 32 53

@tkcranny tkcranny requested a review from a team as a code owner October 18, 2024 07:42
@kola-er kola-er merged commit afa1140 into main Oct 21, 2024
14 checks passed
@kola-er kola-er deleted the IQQ-1861-type-zrequest-data branch October 21, 2024 15:51
@rnegron rnegron mentioned this pull request Oct 24, 2024
@@ -138,16 +139,20 @@ type DehydrateFunc = <T>(
export interface ZObject {
request: {
// most specific overloads go first
(
Copy link
Contributor

Choose a reason for hiding this comment

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

@tkcranny I wonder if the default better be unknown, so that we don't effectively disable type checking for all downstream code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants