createPrecompiledValidator() returns its validator class
Detect
- Your code stores the result of
createPrecompiledValidator()in a variable or state and later assigns it another validator, such as one fromcustomizeValidator(). - The type checker reports
TS2740: Type 'AJV8Validator<JSONSchema7, object>' is missing the following properties from type 'AJV8PrecompiledValidator<JSONSchema7, object>': rootSchema, validateFns, mainValidator, schemaContext, and 3 moreon that assignment.
Change
createPrecompiledValidator() returns its class, AJV8PrecompiledValidator from @rjsf/validator-ajv8 or ATAPrecompiledValidator from @rjsf/validator-ata. A variable or state it initializes takes that class as its type. Annotate the variable as ValidatorType<S, F>, or pass it as the type argument of useState<ValidatorType>():
-let validator = createPrecompiledValidator(validateFns, schema);
+let validator: ValidatorType = createPrecompiledValidator(validateFns, schema);
validator = customizeValidator();
Verify
- The reassignment type-checks.
- The annotation types the
rawValidation()errors asunknown[]again, where the class givesErrorObject[]. See rawValidation() returns the validator's error type if you read them.
Also read: