Skip to content

issue with schema : 'impossible to assign type' #2

@bcotteret

Description

@bcotteret

Hello,
I'm trying to implement a schema for my settings in my electron application
The app can lauch in dev, but fails to bundle in production

npm run dev  ## works
npm run build:linux  ## fails

Firstly, I've tried with you example

import { Conf } from "electron-conf/main";
const schema = {
  type: "object",
  properties: {
    locale: {
      type: "string",
      maxLength: 2,
      nullable: false,
      default: "fr"
    },
    access_token: {
      type: "string",
      nullable: true
    },
    user: {
      type: "object"
    },
    barcodePrinter: {
      type: "string",
      nullable: true
    },
    textPrinter: {
      type: "string",
      nullable: true
    }
  }
}
const conf = new Conf({ schema })
export { conf };

But then I got a Typescript issue

src/main/settings.ts:40:25 - error TS2322: Type '{ type: string; properties: { locale: { type: string; maxLength: number; nullable: boolean; default: string; }; access_token: { type: string; nullable: boolean; }; user: { type: string; }; barcodePrinter: { ...; }; textPrinter: { ...; }; }; }' is not assignable to type 'JSONSchema<Record<string, unknown>> | undefined'.
  Type '{ type: string; properties: { locale: { type: string; maxLength: number; nullable: boolean; default: string; }; access_token: { type: string; nullable: boolean; }; user: { type: string; }; barcodePrinter: { ...; }; textPrinter: { ...; }; }; }' is not assignable to type '({ anyOf: readonly UncheckedJSONSchemaType<Record<string, unknown>, false>[]; } & { [keyword: string]: any; $id?: string | undefined; $ref?: string | undefined; $defs?: Record<...> | undefined; definitions?: Record<...> | undefined; }) | ({ ...; } & { ...; })'.
    Type '{ type: string; properties: { locale: { type: string; maxLength: number; nullable: boolean; default: string; }; access_token: { type: string; nullable: boolean; }; user: { type: string; }; barcodePrinter: { ...; }; textPrinter: { ...; }; }; }' is not assignable to type '{ oneOf: readonly UncheckedJSONSchemaType<Record<string, unknown>, false>[]; } & { [keyword: string]: any; $id?: string | undefined; $ref?: string | undefined; $defs?: Record<...> | undefined; definitions?: Record<...> | undefined; }'.
      Property 'oneOf' is missing in type '{ type: string; properties: { locale: { type: string; maxLength: number; nullable: boolean; default: string; }; access_token: { type: string; nullable: boolean; }; user: { type: string; }; barcodePrinter: { ...; }; textPrinter: { ...; }; }; }' but required in type '{ oneOf: readonly UncheckedJSONSchemaType<Record<string, unknown>, false>[]; }'.

40 const conf = new Conf({ schema })
                           ~~~~~~

  node_modules/ajv/dist/types/json-schema.d.ts:25:5
    25     oneOf: readonly UncheckedJSONSchemaType<T, IsPartial>[];
           ~~~~~
    'oneOf' is declared here.


Found 1 error in src/main/settings.ts:40

I also tried this : (using ajv to enforce type)
I understood ajv is an underlying piece in schema validation
So I took their example

import { JSONSchemaType } from 'ajv'
import { Conf } from "electron-conf/main";

interface Settings {
  locale: string,
  access_token: string,
  user: object,
  barcodePrinter: string,
  textPrinter: string
}

const schema: JSONSchemaType<Settings> = {
  type: "object",
  properties: {
    locale: {
      type: "string",
      maxLength: 2,
      nullable: false,
      default: "fr"
    },
    access_token: {
      type: "string",
      nullable: true
    },
    user: {
      type: "object"
    },
    barcodePrinter: {
      type: "string",
      nullable: true
    },
    textPrinter: {
      type: "string",
      nullable: true
    }
  }
}
const conf = new Conf({ schema })
export { conf };

Then I got this error,

src/main/settings.ts:12:7 - error TS2322: Type '{ type: "object"; properties: { locale: { type: "string"; maxLength: number; nullable: false; default: string; }; access_token: { type: "string"; nullable: true; }; user: { type: "object"; }; barcodePrinter: { ...; }; textPrinter: { ...; }; }; }' is not assignable to type 'JSONSchemaType<Settings>'.
  Type '{ type: "object"; properties: { locale: { type: "string"; maxLength: number; nullable: false; default: string; }; access_token: { type: "string"; nullable: true; }; user: { type: "object"; }; barcodePrinter: { ...; }; textPrinter: { ...; }; }; }' is not assignable to type '({ anyOf: readonly UncheckedJSONSchemaType<Settings, false>[]; } & { [keyword: string]: any; $id?: string | undefined; $ref?: string | undefined; $defs?: Record<...> | undefined; definitions?: Record<...> | undefined; }) | ({ ...; } & { ...; }) | ({ ...; } & ... 2 more ... & { ...; })'.
    Type '{ type: "object"; properties: { locale: { type: "string"; maxLength: number; nullable: false; default: string; }; access_token: { type: "string"; nullable: true; }; user: { type: "object"; }; barcodePrinter: { ...; }; textPrinter: { ...; }; }; }' is not assignable to type '{ type: "object"; additionalProperties?: boolean | UncheckedJSONSchemaType<unknown, false> | undefined; unevaluatedProperties?: boolean | UncheckedJSONSchemaType<unknown, false> | undefined; ... 7 more ...; maxProperties?: number | undefined; } & { ...; } & { ...; } & { ...; }'.
      Type '{ type: "object"; properties: { locale: { type: "string"; maxLength: number; nullable: false; default: string; }; access_token: { type: "string"; nullable: true; }; user: { type: "object"; }; barcodePrinter: { ...; }; textPrinter: { ...; }; }; }' is not assignable to type '{ type: "object"; additionalProperties?: boolean | UncheckedJSONSchemaType<unknown, false> | undefined; unevaluatedProperties?: boolean | UncheckedJSONSchemaType<unknown, false> | undefined; ... 7 more ...; maxProperties?: number | undefined; }'.
        The types of 'properties.access_token' are incompatible between these types.
          Type '{ type: "string"; nullable: true; }' is not assignable to type '{ $ref: string; } | (UncheckedJSONSchemaType<string, false> & { nullable?: false | undefined; const?: string | undefined; enum?: readonly string[] | undefined; default?: string | undefined; })'.
            Type '{ type: "string"; nullable: true; }' is not assignable to type '({ anyOf: readonly UncheckedJSONSchemaType<string, false>[]; } & { [keyword: string]: any; $id?: string | undefined; $ref?: string | undefined; $defs?: Record<string, UncheckedJSONSchemaType<...>> | undefined; definitions?: Record<...> | undefined; } & { ...; }) | ({ ...; } & ... 1 more ... & { ...; }) | ({ ...; } &...'.
              Type '{ type: "string"; nullable: true; }' is not assignable to type '{ type: "string"; } & StringKeywords & { allOf?: readonly UncheckedPartialSchema<string>[] | undefined; anyOf?: readonly UncheckedPartialSchema<string>[] | undefined; ... 4 more ...; not?: UncheckedPartialSchema<...> | undefined; } & { ...; } & { ...; }'.
                Type '{ type: "string"; nullable: true; }' is not assignable to type '{ nullable?: false | undefined; const?: string | undefined; enum?: readonly string[] | undefined; default?: string | undefined; }'.
                  Types of property 'nullable' are incompatible.
                    Type 'true' is not assignable to type 'false'.

12 const schema: JSONSchemaType<Settings> = {
         ~~~~~~

Any idea or hint ?
Or can I by pass this TS problem ?

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions