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

skipLibCheck or DOM lib required #999

Open
chrisguttandin opened this issue Dec 27, 2024 · 3 comments
Open

skipLibCheck or DOM lib required #999

chrisguttandin opened this issue Dec 27, 2024 · 3 comments
Assignees
Labels
enhancement New feature or request question Further information is requested

Comments

@chrisguttandin
Copy link

When using Valibot with TypeScript in a project targeting Node.js it is currently either necessary to set 'skipLibCheck' to true or to include the 'DOM' lib.

This is the error I ran into:

node_modules/valibot/dist/index.d.ts:682:95 - error TS2304: Cannot find name 'File'.

682 interface FileSchema<TMessage extends ErrorMessage<FileIssue> | undefined> extends BaseSchema<File, File, FileIssue> {
                                                                                                  ~~~~

node_modules/valibot/dist/index.d.ts:682:101 - error TS2304: Cannot find name 'File'.

682 interface FileSchema<TMessage extends ErrorMessage<FileIssue> | undefined> extends BaseSchema<File, File, FileIssue> {

What about wrapping the usage of File with a type helper to ensure it compiles successfully? I think something like this should work:

type GlobalType<Type extends string> = Type extends keyof (typeof globalThis) ? (typeof globalThis)[Type] : never;

interface FileSchema<TMessage extends ErrorMessage<FileIssue> | undefined>
    extends BaseSchema<
        GlobalType<'File'>,
        GlobalType<'File'>,
        FileIssue
    >
{
   /// ...
}

As a result Valibot could be used without setting 'skipLibCheck' or adding the 'DOM' lib. And on top of that file() would be unusable when using Valibot with Node.js as expected.

Please let me know if you like the solution above. I would be happy to create a PR for it.

@fabian-hiller
Copy link
Owner

fabian-hiller commented Dec 27, 2024

Thank you for creating this issue. Why is enabling skipLibCheck or adding 'DOM' a problem for you? Unfortunately, File is not the only API we use. Other types such as TextEncoder and Blob would cause similar problems.

We only use Web APIs that are supported across different runtimes. Strange that this still causes problems at the type level.

@fabian-hiller fabian-hiller self-assigned this Dec 27, 2024
@fabian-hiller fabian-hiller added enhancement New feature or request question Further information is requested labels Dec 27, 2024
@chrisguttandin
Copy link
Author

Thanks a lot for your quick reply.

As far as I can tell File is the only thing used by Valibot unavailable on Node.js. Blob (https://nodejs.org/docs/v22.12.0/api/globals.html#class-blob) and TextEncoder (https://nodejs.org/docs/v22.12.0/api/globals.html#textencoder) are for example implemented.

It might be padentic but I don't like adding the 'DOM' lib when developing something only meant for Node.js. I think it kind of defeats the purpose of using TypeScript as it adds type definitions for things which aren't available on Node.js like File. When using it TypeScript will signal everything is fine and can't help to prevent runtime errors.

I just double checked that 'skipLibCheck' has a similar effect. It happily lets you use file() and just marks the result as any instead of File. It makes all of Valibot's validation silently go away.

@fabian-hiller
Copy link
Owner

File should be available in Node.js >=v20 according to MDN.

It might be padentic but I don't like adding the 'DOM' lib when developing something only meant for Node.js. I think it kind of defeats the purpose of using TypeScript...

I agree! This is not a good solution.

I just double checked that 'skipLibCheck' has a similar effect. It happily lets you use file() and just marks the result as any instead of File. It makes all of Valibot's validation silently go away.

Is this solution okay? At least I haven't had any problems with it so far.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants