question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Typescript: impossible to use JSONSchemaType with mixed index signatures and defined properties

See original GitHub issue

ajv version: 8.2.0

interface T {
  a: number;
  [key: string]: unknown;
}

const b: JSONSchemaType<T> = {
  type: 'object',
  properties: { a: { type: 'number' } },
  required: ['a'],
  additionalProperties: true, // doesn't affect anything
};

ts error

Type '{ type: "object"; properties: { a: { type: "number"; }; }; required: string[]; }' is not assignable to type 'JSONSchemaType<T, false>'.
  Type '{ type: "object"; properties: { a: { type: "number"; }; }; required: string[]; }' is not assignable to type '{ type: "object"; additionalProperties?: boolean | JSONSchemaType<unknown, false> | undefined; unevaluatedProperties?: boolean | JSONSchemaType<unknown, false> | undefined; ... 7 more ...; maxProperties?: number | undefined; } & { ...; } & { ...; } & { ...; }'.
    Type '{ type: "object"; properties: { a: { type: "number"; }; }; required: string[]; }' is not assignable to type '{ type: "object"; additionalProperties?: boolean | JSONSchemaType<unknown, false> | undefined; unevaluatedProperties?: boolean | JSONSchemaType<unknown, false> | undefined; ... 7 more ...; maxProperties?: number | undefined; }'.
      Types of property 'properties' are incompatible.
        Type '{ a: { type: "number"; }; }' is not assignable to type 'PropertiesSchema<T>'.
          Property 'a' is incompatible with index signature.
            Type '{ type: "number"; }' is not assignable to type '{ $ref: string; } | (JSONSchemaType<unknown, false> & { nullable: true; const?: undefined; enum?: readonly unknown[] | undefined; default?: unknown; })'.
              Type '{ type: "number"; }' is not assignable to type '{ oneOf: readonly JSONSchemaType<unknown, false>[]; } & { [keyword: string]: any; $id?: string | undefined; $ref?: string | undefined; $defs?: { [x: string]: JSONSchemaType<Known, true> | undefined; } | undefined; definitions?: { ...; } | undefined; } & { ...; }'.
                Property 'oneOf' is missing in type '{ type: "number"; }' but required in type '{ oneOf: readonly JSONSchemaType<unknown, false>[]; }'.

I’ve tried just about everything to make it work, only solution right now is to pass a different, less defined, type to JSONSchemaType.

Things I’ve tried

  • typecasting the schemas
  • defining the additional properties as { type: ‘string’ }
  • all manners of redefining the index signature

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
epoberezkincommented, Jul 24, 2021

Going to close it.

The main reason to use JSONSchemaType is to avoid typos in large but common schemas. It doesn’t have to be used for all schemas, and some typescript types would always remain out of scope.

0reactions
erikbrinkmancommented, Jul 18, 2021

I’m intrigued by this, however there are typescript limitations with implementing it. If you define type Keys = keyof T what do you get? string | number. I’m not going to go into why, but from an inspection perspective, it’s not possible for JSONSchemaType to determine the explicitly named properties afaik.

The reverse should be possible. I’m not sure if JTDDataType supports it, but it probably should.

Read more comments on GitHub >

github_iconTop Results From Across the Web

typescript - How to mix index signature with known properties?
What you can do is to separate the known and unknown properties in two interfaces and define your target type as a union...
Read more >
Managing objects with unknown structures in TypeScript using ...
We look into various options on how to handle an object that has a structure we don't know. We use index signatures and...
Read more >
json-schema-to-typescript | Yarn - Package Manager
Custom schema properties: tsType : Overrides the type that's generated from the schema. Useful for forcing a type to any or when using...
Read more >
Index Signatures in TypeScript - Dmitri Pavlutin
The answer is to use an index signature! Let's find what are TypeScript index signatures and when they're needed.
Read more >
aws-cdk/aws-eks module - AWS Documentation
Quick Start. This example defines an Amazon EKS cluster with the following configuration: Dedicated VPC with default configuration (Implicitly created using ec2 ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found