grantila / suretype

Typesafe JSON (Schema) validator
502 stars 9 forks source link

Object validator not type checking as expected #40

Open saramcicchi opened 11 months ago

saramcicchi commented 11 months ago

Say we have the following TS interface

interface Fish {
   name: string;
}

If you create an object of that type and give it extra properties, Typescript throws an error.

let fish: Fish = {
   name: 'Bubbles',
   numberOfLegs: 2
};

Type '{ name: string; numberOfLegs: number; }' is not assignable to type 'Fish'.
Object literal may only specify known properties, and 'numberOfLegs' does not exist in type 'Fish'.

However, if you create an object validator with extra properties, no type error occurs.

let fishValidator: ObjectValidator<Fish> = v.object({
   name: v.string().required(),
   numberOfLegs: v.number().required()
});

I would expect the same error to be reported if extra properties exist in the object validator definition but do not exist in the specified type. Am I misunderstanding something here? Thanks in advance!