hapijs / joi

The most powerful data validation library for JS
Other
20.75k stars 1.51k forks source link

TS won't compile when using array fields in a strict object schema #2999

Closed onlyice closed 7 months ago

onlyice commented 8 months ago

Support plan

Context

What are you trying to achieve or the steps to reproduce?

The following code produced a TypeScript error:

class User {
  addresses: string[];

  constructor() {
    this.addresses = [];
  }
}

const UserSchema = Joi.object<User, true>({
  addresses: Joi.array().items(Joi.string()),
});

What was the result you got?

The TS compiler reports the following:

TS2322: Type ArraySchema<any[]> is not assignable to type BinarySchema<Buffer>

I'm using TypeScript 5.2.2 with the following tsconfig.json:

{
  "compilerOptions": {
    "skipLibCheck": true,
    "jsx": "react",
    "target": "esnext",
    "module": "esnext",
    "importHelpers": true,
    "noImplicitAny": true,
    "esModuleInterop": true,
    "moduleResolution": "node",
    "baseUrl": "./",
    "allowSyntheticDefaultImports": true,
    "strict": true,
  }
}

What result did you expect?

I would expect the code to compile without issues.

Marsup commented 8 months ago

This error seems to happen only when @types/node is not loaded, is this intentional?

onlyice commented 8 months ago

This error seems to happen only when @types/node is not loaded, is this intentional?

My project is a pure client React project, so there is no need to use @types/node.

After adding @types/node into dev dependencies, the issue is fixed. Thanks!

J053Fabi0 commented 2 months ago

How can this be solved in Deno?

Marsup commented 2 months ago

Does it work using the same solution?

J053Fabi0 commented 2 months ago

No, it does not

Marsup commented 2 months ago

Exact same error or something else?

J053Fabi0 commented 2 months ago

Yes.

Property 'encoding' is missing in type 'ArraySchema<any[]>' but required in type 'BinarySchema<Buffer>'.deno-ts(2741)
Marsup commented 2 months ago

You'll have to be more specific than that, I tried a quick codespace with deno 1.43 and it worked out of the box with the code above.