TCMiranda / joi-extract-type

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

Arrays lose all type information if they are allowed to be null #42

Open lonewarrior556 opened 3 years ago

lonewarrior556 commented 3 years ago

simple example

import Joi from '@hapi/joi';
import 'joi-extract-type';

var schema = Joi.object({
  one: Joi.array().items({ a: Joi.string().required() }),
  two: Joi.array().items({ a: Joi.string().required() }).allow(null),
});

type Extracted = Joi.extractType<typeof schema>;

image

packages

    "@hapi/joi": "15.1.1",
    "joi-extract-type": "15.0.8",
    "@types/hapi__joi": "15.0.4",
i-kay commented 3 years ago

Also, when writing Joi code like this Joi.array().items(Joi.string()).required().allow(null), it infers (string | null)[] but I think it would be string[] | null.

image