TCMiranda / joi-extract-type

Provides native type extraction from Joi schemas for Typescript
MIT License
151 stars 27 forks source link

Objects always interpreted as optional when inside an object #27

Closed bspot closed 4 years ago

bspot commented 4 years ago

Similar to #12, objects are interpreted as optional when inside an object, even when they are marked as required() or exist().

Code:

import * as Joi from '@hapi/joi'
import 'joi-extract-type'

const validator = Joi.object().required().keys({
  nested: Joi.object().required().keys({
    foo: Joi.string().required()
  })
})

type ResultType = Joi.extractType<typeof validator>
/*
ResultType is 
Map<{
  nested?: Map<{} & {
    foo: string;
  }> | undefined; } & {}>
*/

// No error here
const x: ResultType = {}

// TS2532: Object is possibly 'undefined'.
console.log(x.nested.foo)

Versions:

lonewarrior556 commented 4 years ago

@bspot This is an issue with Joi 16. examples works on Joi 15 https://github.com/TCMiranda/joi-extract-type/issues/22