maghis / types-joi

TypeScript types for joi (Object schema description language and validator for JavaScript objects) https://github.com/hapijs/joi
28 stars 4 forks source link

Type 'string' is not assignable to type '"string" | "number" | "boolean" | "undefined" | "object" | "function"' #1

Closed isaachinman closed 5 years ago

isaachinman commented 5 years ago

Hi there - very interesting idea here! Unfortunately I'm unable to get your project to compile after installing types-joi and copy/pasting the example from your test suite:

const schema = joi
  .object({
    test: joi
      .string()
      .required()
      .default('baz')
      .valid('baz' as const, 'bar' as const),
    val: joi.number().optional(),
    inner: joi
      .object({
        a: joi.string(),
        b: joi.number(),
      })
      .required(),
    values: joi
      .array()
      .required()
      .items(joi.string().required()),
    buf: joi.binary().required(),
  })
  .required();

type SchemaInterface = InterfaceFrom<typeof schema>;

I get the following error:

Type 'ObjectSchema' does not satisfy the constraint 'BaseSchema<any>'.
  Types of property 'schemaType' are incompatible.
    Type 'string' is not assignable to type '"string" | "number" | "boolean" | "undefined" | "object" | "function"'.

I'm curious if you're using this package in any active projects? Is this an error on my part?

maghis commented 5 years ago

@isaachinman yes, it's being used by multiple projects.

Please, make sure you are using at least TypeScript 3.4

isaachinman commented 5 years ago

Yes, we're up to date on our TS dep. After taking a closer look at the docs, I realised you expect users to import joi from this package. I had completely missed that on the first pass over - I imagine many other people will miss this too if it's not explicitly stated:

import * as joi from "types-joi";

Although I haven't had a chance to retry my use case with this new knowledge, I will close this issue for now and come back later if anything arises.

Thanks, and best of luck!