Skip to content

Strict object schema with a boolean property expects alternative, not boolean #2848

@joshkel

Description

@joshkel

Support plan

  • is this issue currently blocking your project? (yes/no): no
  • is this issue affecting a production system? (yes/no): no

Context

  • node version: 14.19.3
  • module version with issue: 17.6.1
  • last module version without issue: 17.6.0
  • environment (e.g. node, browser, native): Node
  • used with (e.g. hapi application, another framework, standalone, ...): Hapi
  • any other relevant information: TypeScript 4.8.2

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

Joi 17.6.1's addition of AlternativesSchema for a strict object map has caused TypeScript to start reporting an error for boolean properties.

interface Test {
  enabled: boolean;
}

Joi.object<Test, true>({
  enabled: Joi.boolean(),
});

What was the result you got?

error TS2739: Type 'BooleanSchema' is missing the following properties from type 'AlternativesSchema': conditional, match, try

What result did you expect?

Successful compilation.

The problem appears to be that IsUnion returns true for booleans. I think that this may be because TypeScript treats boolean as the union of true and false (see the mentions of 'boolean' within checker.ts), but I'm really not sure. See also Stack Overflow's discussion on the limitations of an IsUnion implementation.

To fix it, maybe reorder ObjectPropertiesSchema to check boolean before union?

TypeScript playground link for experimenting with IsUnion implementations

(I'm not sure about ObjectPropertiesSchema's union check for another reason: It thinks that interface Test { value: 'a' | 'b' } should use an AlternativesSchema, but it seems like a more idiomatic Joi implementation would be Joi.string().valid('a', 'b'). That's a separate issue and not currently affecting me, but I wanted to mention it in case it affects how this issue should be addressed. Thanks!)

Metadata

Metadata

Assignees

Labels

bugBug or defectsupportQuestions, discussions, and general supporttypesTypeScript type definitions

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions