Solution 1:

In JavaScript this:

required().default(1)

is the same as this:

required()["default"](1)

because you can access object properties either as:

object["propertyName"]

or:

object.propertyName

(with certain restrictions in the second case).

So it's strange that TypeScript would output the longer style if it doesn't have to, but it's also strange that the longer style doesn't work exactly the same as the shorter one.

I would try to manually change the compiled JavaScript to the shorter version and see if that helps. If it doesn't then the problem is somewhere else. My suspicion is that it will not help.

The .default() should work in TypeScript because it is defined in @types/joi - see:

  • https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/joi/index.d.ts#L272-L273

But on the other hand there is this comment:

// TODO express type of Schema in a type-parameter (.default, .valid, .example etc)

Which may suggest that .default() implementation is not ready yet - see:

  • https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/joi/index.d.ts#L6

and also there's this issue: joi.d.ts out of date, missing types

  • https://github.com/DefinitelyTyped/DefinitelyTyped/issues/9332