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

Usage in JavaScript #168

Closed
julianomqs opened this issue Sep 25, 2020 · 5 comments
Closed

Usage in JavaScript #168

julianomqs opened this issue Sep 25, 2020 · 5 comments

Comments

@julianomqs
Copy link

julianomqs commented Sep 25, 2020

Hi,

Can this library be used with pure JavaScript (no TypeScript)?

I read the documentation and despite the strictNullChecks requirement and types, it doesn't says can't be used with JavaScript.

I made a test here in a mixed JavaScript/TypeScript project without strictNullChecks and it worked.

I was thinking about using the library in a pure JavaScript project, so I would like to see your opinion and recommendations on the matter, possibly mentioning in the docs.

Thanks.

@colinhacks
Copy link
Owner

The strictNullChecks is required for proper static typing, so you're correct that it doesn't affect the runtime execution. There are many closed issues on this repo that will shed some light on the sort of type inference issues you'll experience without strictNullChecks. The biggest issue is that all object properties will be inferred as optional by the type checker.

You can use this in a hybrid/JavaScript project just fine. I'll add this to the readme to avoid more confusion down the road 👍

@websocket98765
Copy link

websocket98765 commented Mar 21, 2021

Were you able to get this working in a JavaScript project? I'm not accustom to packages being distributed as typescript and it errors my project when trying to use it in a JavaScript project:

ENOENT: no such file or directory, open '/Users/me/my-app/node_modules/zod/src/index.ts'

If I understand correctly, I'd have to add typescript processing to my app's build pipeline in order to use this, no? Would be great if JS version were distributed too please.

@colinhacks
Copy link
Owner

@websocket98765 what version of Zod do you have installed?

@websocket98765
Copy link

3.0.0-alpha.24

zod's package.json:

  "name": "zod",
  "version": "3.0.0-alpha.24",
  "description": "TypeScript-first schema declaration and validation library with static type inference",
  "main": "./lib/cjs/index.js",
  "types": "./lib/cjs/index.d.ts",
  "module": "./lib/esm/index.js",
  "exports": {
    "require": "./lib/cjs/index.js",
    "import": "./lib/esm/index.js"
  },

I imported it via import * as z from 'zod';.

Zod's package.json's "main" is a JavaScript file, so maybe this isn't an issue with zod. It could be something in my build system going wonky--using vite ^2.1.0 via SvelteKit (beta). It's weird that the error makes it appear like it's trying to bring in a ts file. I might mention this to the Vite team.

@colinhacks
Copy link
Owner

Ah yeah I've been hearing about Vite more and more. Seems really interesting. Though I agree this is probably a bug on their end. I have no idea why it would be trying to import src/index.ts.

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

No branches or pull requests

3 participants