Giter Club home page Giter Club logo

Comments (12)

TahsinAyman avatar TahsinAyman commented on June 9, 2024

ill try to fix this issue please

from zod.

JacobWeisenburger avatar JacobWeisenburger commented on June 9, 2024

image

from zod.

TahsinAyman avatar TahsinAyman commented on June 9, 2024

thanks for explaining let me fix it
it would be appreciated if i were assigned in this task

from zod.

hesxenon avatar hesxenon commented on June 9, 2024

@JacobWeisenburger I'm not sure I follow, what's your point here?

It seems you've disabled exactOptionalPropertyTypes in your screenshot, hence the compiler adds undefined, but other than that I don't see what you're getting at :/

from zod.

JacobWeisenburger avatar JacobWeisenburger commented on June 9, 2024

Here's my tsconfig.json. It doesn't have exactOptionalPropertyTypes specified.

{
    "compilerOptions": {
        "lib": [
            "dom",
            "dom.iterable",
            "esnext"
        ],
        "module": "esnext",
        "target": "esnext",
        "moduleResolution": "bundler",
        "moduleDetection": "force",
        "allowImportingTsExtensions": true,
        "noEmit": true,
        "composite": true,
        "strict": true,
        "downlevelIteration": true,
        "skipLibCheck": true,
        "jsx": "preserve",
        "allowSyntheticDefaultImports": true,
        "forceConsistentCasingInFileNames": true,
        "allowJs": true,
        "types": [
            "bun-types"
        ]
    },
    "include": [
        "src/**/*",
        "package.json"
    ]
}

from zod.

hesxenon avatar hesxenon commented on June 9, 2024

yeah? The default is false; that doesn't make it any more correct for zod to take away the decision from the compiler for those that have it set to true.

Imho this isn't an enhancement but a bug - zod "deliberately" add's implicit nullability even if that nullability is explicitly forbidden. From what I'm reading from other issues in this repo I'm fairly certain that this is not what zod wanted or intended.


fyi: @types/bun exists by now, just in case.

from zod.

TahsinAyman avatar TahsinAyman commented on June 9, 2024

i understood the problem and i also looked after the source code and understood what's wrong
let me try to fix it

from zod.

hesxenon avatar hesxenon commented on June 9, 2024

from zod.

TahsinAyman avatar TahsinAyman commented on June 9, 2024

sorry im just a beginner.

from zod.

hesxenon avatar hesxenon commented on June 9, 2024

the more I look at this, the more it looks like this won't be possible without a breaking change. While I did get correct optional inference working (basically just switch addQuestionMarks and baseObjectOutputType and infer Required Key with extends ZodOptional) the core of the problem is that ZodOptional adds |undefined to its in and output.

Removing that assumption breaks other tests because optional can't depend on its wrapping context.

E.g. here

const stringSchema = z.string().optional();

const objectSchema = z.object({
  foo: stringSchema
});

type S = z.infer<typeof stringSchema> // expectation: string | undefined
type O = z.infer<typeof objectSchema> // expectation: { foo?: string }

Since the stringschema can't know when it's being used in an object context it's impossible to correctly represent optional keys without also making the value "undefinedable".

Imho the only true solution (and maybe even the right one) would be to strictly reserve .optional() for usage within objects and to have a new operation like .nullable(), e.g. .orUndefined() for everything else.

@colinhacks , sorry for directly mentioning you but do you see any other solution?

from zod.

TahsinAyman avatar TahsinAyman commented on June 9, 2024

🔥 I also tried but it didn't seem to make any sense to me 👎🏻

from zod.

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.