Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot set logger: false in call to constructor #2345

Closed
jmozmoz opened this issue Nov 14, 2023 · 1 comment
Closed

Cannot set logger: false in call to constructor #2345

jmozmoz opened this issue Nov 14, 2023 · 1 comment

Comments

@jmozmoz
Copy link

jmozmoz commented Nov 14, 2023

What version of Ajv are you using? Does the issue happen if you use the latest version?

ajv@8.12.0

Your typescript code

import Ajv from 'ajv';
const options = {strict: false, validateSchema: false, logger: false};
const ajv = new Ajv(options);

Typescript compiler error messages

content/endpoint-manager.ts:3:21 - error TS2345: Argument of type '{ strict: boolean; validateSchema: boolean; logger: boolean; }' is not assignable to parameter of type 'Options'.
  Type '{ strict: boolean; validateSchema: boolean; logger: boolean; }' is not assignable to type 'CurrentOptions'.
    Types of property 'logger' are incompatible.
      Type 'boolean' is not assignable to type 'false | Logger'.

3 const ajv = new Ajv(options);

The following code compiles without error:

import Ajv from 'ajv';
const ajv = new Ajv({strict:false, validateSchema: false, logger: false});

This one also:

import Ajv from 'ajv';
declare const Components: any;
Components.utils.import('resource://gre/modules/Console.jsm');

const options = {strict: false, validateSchema: false, logger: console};
const ajv = new Ajv(options);

Describe the change that should be made to address the issue?
Accept false as setting for logger as described in the documentation:
https://ajv.js.org/options.html#logger

Are you going to resolve the issue?
Sorry, I do not know how.

@jasoniangreen
Copy link
Collaborator

Hi @jmozmoz, this is a limitation of TypeScript and not related to AJV.

The reason it happens is AJV requires this type be false | Logger. When you declare an object with a property of false though, TypeScript interprets that as boolean. I'm not sure why exactly, maybe the issues is that objects are mutable so TS can't be sure it will always be false? In any case it can be fixed by adding as const to your options object or as false to the false value.

const options = {strict: false, validateSchema: false, logger: false} as const; or logger: false as false

As this is a TS peculiarity I will close this ticket but thanks for sharing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants
@jmozmoz @jasoniangreen and others