superstruct icon indicating copy to clipboard operation
superstruct copied to clipboard

Describe failed on intersection or inheritance

Open PoyangLiu opened this issue 3 years ago • 5 comments

The following two scenario doesn't seem to work when using Describe. Both of them says

Type 'Struct<{ ... }>' is not assignable to type 'Describe<IMyInterface>'.
Types of property 'TYPE' are incompatible.
// inheritance
interface Child extends Parent { ... };
const childValidator: Describe = object({
    ...parentValidator.schema,
    childField: string(),
});
// intersection
interface Child  = Parent & { ... };
const childValidator: Describe = intersection([
    parentValidator,
    object({
        childField: string(),
    })
]);

PoyangLiu avatar Nov 09 '21 03:11 PoyangLiu

@ianstormtaylor I'm also experiencing this.

The issue seems to arise when using optional properties.

interface Pet {
  name: string
  owner?: string
}

interface Dog extends Pet {
  breed: string
}

const petSchema: s.Describe<Pet> = s.type({
  name: s.string(),
  owner: s.optional(s.string()),
})

export const dogSchema: s.Describe<Dog> = s.type({
  ...petSchema.schema,
  breed: s.string(),
})

// type error: Types of property TYPE are incompatible

R-Bower avatar Sep 30 '22 20:09 R-Bower

@R-Bower hmm do you know what’s going on? What’s the minimum amount of code that will reproduce it?

ianstormtaylor avatar Sep 30 '22 20:09 ianstormtaylor

Not entirely sure. The example that I included is close to minimal.

R-Bower avatar Sep 30 '22 21:09 R-Bower

You may also need to enable strict mode in your tsconfig. The errors sometimes don't show when strict is set to false.

R-Bower avatar Sep 30 '22 22:09 R-Bower

@ianstormtaylor hosted a reproduction of the error at https://github.com/R-Bower/superstruct-describe-error-reproduction

R-Bower avatar Oct 03 '22 18:10 R-Bower