question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Type Inference : Named Types with nested schemas in objects

See original GitHub issue

Hey everyone! I’ve recently discovered Zod and this is quite a marvel! Congrats @colinhacks and every contributor for the awesome work !

I’ve got an “issue” that can be quite annoying when dealing with complex objects, let me explain with this short example :

const foo = z.object({
    name: z.string(),
    birthdate: z.date()
})
type Foo = z.infer<typeof foo>

const bar = z.object({
    property: z.string(),
    foo
})
type Bar = z.infer<typeof bar>

Here i defined two schemas, a practical example would be : Foo is a Person, and Bar is an Order or something, it doesn’t matter. When I infer the type of Bar, I would like the final output to look like this :

type Bar = {
    property: string;
    foo: Foo;
}

Of course right now, this is impossible, I’ve searched the docs and tried a few things, but unfortunately nothing worked.

What the inferred type looks like (no surprise here)

type Bar = {
    property: string;
    foo: {
        name: string;
        birthdate: Date;
    };
}

What i’ve tried so far, without any luck

const bar = z.object({
    property: z.string(),
    foo: foo as z.ZodSchema<Foo>
})
const bar = z.object({
    property: z.string(),
    foo: foo.transform((arg): Foo => { return arg })
})

My terrible workaround

type Bar = { property: string, foo: Foo }

Why I consider it important With large DB infrastructures, having understandable and clear typings is essential, while Zod allows me to really enforce typing, I would really like to have a clear type hinting from VSCode when using z.infer<T>. The workaround is fine but it’s static.

Have anyone figured out how to achieve such behavior ?

Cheers! Julien

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
OnkelTemcommented, May 14, 2022

So far no solution was provided. While there is one, of course not that shiny.

import { z } from "zod";

type Foo = {
  name: string;
  birthdate: Date
}
const foo: z.ZodType<Foo> = z.object({
    name: z.string(),
    birthdate: z.date()
})

type Bar = {
  property: string;
  foo: Foo;
}
const bar: z.ZodType<Bar> = z.object({
    property: z.string(),
    foo
})

TypeScript Playground link

0reactions
utkuturunccommented, Aug 18, 2022

another way to tackle this could be to create IDE extensions

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript type inference from mongoose Schema · Issue #9715
Given a type Def which represents a schema definition, Entity works by mapping each of its fields into a "non-schema" field (e.g. String...
Read more >
Using Smart Schema to Accelerate Insights from Nested JSON
This blog walks through how Rockset's Smart Schema feature automates schema inference at read time, enabling us to go from complex JSON data, ......
Read more >
Untitled
I'm using the term "schema" to broadly refer to any data type/structure, from a simple `string` to a complex nested object.
Read more >
Polymorphic Type Inference for the Named Nested Relational ...
Abstract. The named nested relational calculus is the canonical query lan- guage for the complex object database model and is equipped with a....
Read more >
typescript - What to do about extremely slow TS type inference ...
For why this works is beyond me, but excluding string before normalizing the schema seems to improve speeds drastically:
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found