grantila/suretype

Object validator not type checking as expected

saramcicchi opened this issue · 0 comments

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!