Coder Social home page Coder Social logo

Comments (16)

zkulbeda avatar zkulbeda commented on May 19, 2024 5

The way it was resolved in kysely:

from valibot.

gbouteiller avatar gbouteiller commented on May 19, 2024 3

It should be related to :

from valibot.

fabian-hiller avatar fabian-hiller commented on May 19, 2024 2

Thank you for the tip. I will try to investigate this in the next few weeks.

from valibot.

fabian-hiller avatar fabian-hiller commented on May 19, 2024 1

Can you send me some code examples?

from valibot.

fabian-hiller avatar fabian-hiller commented on May 19, 2024 1

Yes, that works too. Thank you! Another workaround that works in most cases is intersection:

import { intersection, object, string } from 'valibot';

const Object1 = object({ key1: string() });
const Object2 = object({ key2: string() });
const Object3 = intersection([Object1, Object2]);

from valibot.

fabian-hiller avatar fabian-hiller commented on May 19, 2024 1

Here is a guide that help to choose between intersect and merge: https://valibot.dev/guides/intersections/

from valibot.

fabian-hiller avatar fabian-hiller commented on May 19, 2024 1

I am toying with the idea of removing merge because of this TypeScript problem. Meanwhile, Valibot has a workaround that accomplishes exactly the same thing with a good DX. Please give me feedback.

import * as v from 'valibot';

const ObjectSchema1 = v.object({ key1: v.string() });

const ObjectSchema2 = v.object({ key2: v.number() });

const MergedSchema1 = v.merge([ObjectSchema1, ObjectSchema2]);

// This is the workaround that accomplishes exactly the same thing
const MergedSchema2 = v.object({
  ...ObjectSchema1.entries,
  ...ObjectSchema2.entries,
});

from valibot.

gbouteiller avatar gbouteiller commented on May 19, 2024

I have created a repo with my code.
For context, it's a bunch of schemas for the Notion API as well as a fetch wrapper.
My intent with this library is to validate the API responses and use schema transformations to simplify the structure sent by Notion.

  • The schemas/inputs contains the schemas I constructed and refactored from the API results (Notion API has a lot of union types).
  • The schemas contains the transformations.
  • The problem occurs in page
Capture d’écran 2023-07-29 à 09 14 19
  • If I comment properties which is a record of an union of many schemas, the problem disappears :
export const vIPage = merge([
  vIPageOrDatabaseCommon,
  object({
    object: literal('page'),
    parent: union([vIParentDatabase, vIParentPage, vIParentWorkspace]),
    // properties: record(vIPageProp),
  }),
]);

from valibot.

fabian-hiller avatar fabian-hiller commented on May 19, 2024

Yes, the problem is vIPageProp which is used with properties: record(vIPageProp) in vIPage. However, I have not found out the exact cause yet. I will investigate this further.

from valibot.

fabian-hiller avatar fabian-hiller commented on May 19, 2024

I was able to reproduce the problem. However, I could not find the bug yet. I will investigate further in a moment. It is interesting to note that the problem does not occur in TypeScript v4.9.5. Therefore, it could also be a bug in TypeScript v5.

import { merge, omit, object, union, transform, record, string } from "valibot";

const Merge1 = merge([object({}), object({ key: string() })]);

const Merge2 = merge([Merge1, object({})]);

const Object1 = merge([Merge2, object({})]);

const Object2 = merge([omit(Merge2, ["key"]), object({})]);

const Record = record(union([Object1, Object2]));

transform(Record, (a) => a); // Error: Type instantiation is excessively deep and possibly infinite.ts(2589)

from valibot.

fabian-hiller avatar fabian-hiller commented on May 19, 2024

So as a workaround, you could downgrade TypeScript. It is important that you set the TypeScript version in VS Code to the version in your package.json. If you don't know how to do that and can't figure it out yourself, I can explain it to you here.

from valibot.

fabian-hiller avatar fabian-hiller commented on May 19, 2024

I have thoroughly checked the source code of omit and merge and cannot find any problem. I also noticed that when I copy the source code of merge into the project and update the imports, the problem disappears, even though it is the same code.

Since I have similar problems with Modular Forms using TypeScript v5, I assume that it must be a bug in TypeScript. I can't explain it any other way at the moment. If anyone else wants to investigate the issue further, I'm happy to hear the results. Until then, for all other users, I recommend downgrading TypeScript to v4.9.5.

from valibot.

gbouteiller avatar gbouteiller commented on May 19, 2024

Thank you for your reactivity! Yes, I'll try to downgrade and see if everything works smoothly :) At the same time, I'll investigate about that problem too.

from valibot.

octet-stream avatar octet-stream commented on May 19, 2024

I'm getting the same error message when using with object + merge.

As a workaround I'm using ObjectSchema.object property, like this:

import {object} from "valibot"

import {FileOutput} from "./FileOutput.js"
import {ImageSize} from "./ImageSize.js"

export const ImageOutput = object({
  ...FileOutput.object,

  metadata: ImageSize
})

from valibot.

mutewinter avatar mutewinter commented on May 19, 2024

I noticed marge slowdown in my TypeScript checking performance recently. I tracked it back to this and a switch from merge to intersect (the name has changed since this issue was created) cut my type checking time in half.

Running TypeScript v5.3.3 and Valibot v0.25.0.

from valibot.

fabian-hiller avatar fabian-hiller commented on May 19, 2024

I think this is fixed in v0.30.0

from valibot.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.