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

Still hitting strictNullChekcs must be true in tsconfig #1930

Closed
adriantaut opened this issue Mar 15, 2022 · 10 comments
Closed

Still hitting strictNullChekcs must be true in tsconfig #1930

adriantaut opened this issue Mar 15, 2022 · 10 comments

Comments

@adriantaut
Copy link

adriantaut commented Mar 15, 2022

I am still hitting the error from #1650 and can't pass over it.

Environment:

~ npm list | grep ajv
├── [email protected]
~ npm list | grep tsc
├── [email protected]

~ cat tsconfig.json | grep strictNullCheck
    "strictNullChecks": true,

The ouput of npm run build:

 ~/g/a/2/c/awsiam-configrepo-aws-ppb   *…  cdk  npm run build

> [email protected] build
> tsc

node_modules/ajv/dist/types/json-schema.d.ts:1:92 - error TS1005: '?' expected.

1 declare type StrictNullChecksWrapper<Name extends string, Type> = undefined extends null ? `strictNullChecks must be true in tsconfig to use ${Name}` : Type;
                                                                                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

node_modules/ajv/dist/types/json-schema.d.ts:1:151 - error TS1005: ';' expected.

1 declare type StrictNullChecksWrapper<Name extends string, Type> = undefined extends null ? `strictNullChecks must be true in tsconfig to use ${Name}` : Type;
                                                                                                                                                        ~


Found 2 errors.

I am going to close comments on this issue - it is not really defined what this issue was, and the issue may be that you simply have an incorrect schema or that JSONSchemaType has limitations and cannot be used in your case.

Please submit a new issue with a full code sample from a clear repository once you've checked the following:

  1. strict mode is enabled in tsconfig.json (or at least strictNullChecks)
  2. TS version is >= 4.2.3
  3. you are importing the latest Ajv version (sometimes calling npm install ajv may install the previous version because some other dependency uses it - use npm install ajv@8 and try clean install: rm -rf node_modules && npm I
  4. check that your schema is indeed valid - that required properties are listed in "required", that optional properties have nullable: true, that "type" is set in all schemas - in many cases what was reported as JSONSchemaType issue was a user error where their schema was incorrect - it correctly shown compilation error.
  5. Check other open issues related to JSONSchemaType - there are some known limitations.

A simple temporary workaround to use Ajv without using JSONSchemaType - it is optional.

Originally posted by @epoberezkin in #1650 (comment)

@epoberezkin
Copy link
Member

Can you explain what is the problem? I think the above comment explains it - you need to use strict mode in tsconfig (or at least strictNullChecks)

@adriantaut
Copy link
Author

@epoberezkin the setting is already in place

~ npm list | grep ajv
├── [email protected]
~ npm list | grep tsc
├── [email protected]

~ cat tsconfig.json | grep strictNullCheck
    "strictNullChecks": true,

@epoberezkin
Copy link
Member

Difficult to say what’s wrong with your setup. Possibly there is some other tsconfig somewhere that overrides it. Possibly, you’re importing not what you think you are importing. I would try reproducing the issue from a clean slate (when you have no other dependencies than ajv) and see at what point the problem happens as you add extra things.

@epoberezkin
Copy link
Member

Please re-open or open a new issue with a minimal setup where the issue can be debugged.

@epoberezkin
Copy link
Member

Ah - and looking at your npm ls - the minimum typescript version these type utilities can work with is 4.2.3 and you seem to have 2.0.4?

@ianxm
Copy link

ianxm commented Jul 13, 2022

I had the same problem and the typescript version upgrade fixed it. thanks.

JohannesHoppe added a commit to angular-schule/angular-cli-ghpages that referenced this issue Jul 26, 2022
node_modules/ajv/dist/types/json-schema.d.ts:1:92 - error TS1005: '?' expected.

1 declare type StrictNullChecksWrapper<Name extends string, Type> = undefined extends null ? `strictNullChecks must be true in tsconfig to use ${Name}` : Type;

see ajv-validator/ajv#1930
--> using the same typescript version range as angularfire

engine/engine.ts:272:7 - error TS2794: Expected 1 arguments, but got 0. Did you forget to include 'void' in your type argument to 'Promise'?

272       resolve();
@Cooper1596
Copy link

I still get the strictNullChecks issue even after giving "strictNullChecks" : true in my tsconfig.json file and adding nullable : true in the optionalProperties part of my schema.

I get an error message as follows
Type '{ type: string; items: { type: string; properties: { a: { type: string; }; b: { type: string; }; c: { type: string; }; f: { type: string; }; }; optionalProperties: { e: { type: string; nullable: string; }; d: { type: string; nullable: string; }; }; required: string[]; }; }' is not assignable to type '"strictNullChecks must be true in tsconfig to use JSONSchemaType"'.ts(2322)

I am using electron along with React typescript as my technologies for this desktop app.

I have provided the code for the schema as follows,

import { logger } from "../config/logger";
import Ajv, {JSONSchemaType} from "ajv";
const ajv = new Ajv() 

export const connectionValidator = () => {

    interface ConnectionsModelContents {
        a: string;
        b: string;
        c: string;
        d?: string;
        e?: boolean;
        f: string;
    }

    const connectionSchema: JSONSchemaType<ConnectionsModelContents> = {
        type: "array",
        items: {
            type: "object",
            properties: {
                a: {
                    type: "string"
                },
                b: {
                    type: "string"
                },
                c: {
                    type: "string"
                },
                
                f: {
                    type: "string"
                },
                
            },
            optionalProperties: {
                e: {
                    type: "boolean",
                    nullable: "true"
                },
                d: {
                    type: "string",
                    nullable: "true"
                },
            },
            required: [
                "a",
                "b",
                "c",
                "f"
            ],
        },
    }
    const validation = ajv.compile(connectionSchema);

    if (validation.errors.length > 0) {
        logger.error("connection validation failed")
        logger.error(validation.errors.map((error: any) => error.stack))
        return false
    }

    return true;

}

@epoberezkin
Copy link
Member

@Cooper1596 you need to investigate why a different tsconfig is used to compile the code, not the one you edited.

@epoberezkin
Copy link
Member

Unless there is some other case when this error can be shown?…

@epoberezkin
Copy link
Member

Your schema is invalid though

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

No branches or pull requests

4 participants