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

Add TypeScript type definitions #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

awlayton
Copy link

Sort of resolves #3

@coveralls
Copy link

Coverage Status

Coverage remained the same at 94.86% when pulling df7b8d7 on awlayton:master into 09bea1d on mokkabonna:master.

@Envek
Copy link

Envek commented Feb 5, 2021

@awlayton, thank you! You saved me a plenty of time figuring these typings by myself.

However, when I tried to copy these into my project, I got a bunch of errors from both tsc and eslint:

Expand tsc and eslint output
$ tsc
src/types/json-schema-merge-allof/index.d.ts:6:12 - error TS2314: Generic type 'MergerOptions<Schema>' requires 1 type argument(s).

6   options: MergerOptions
             ~~~~~~~~~~~~~

src/types/json-schema-merge-allof/index.d.ts:22:12 - error TS2314: Generic type 'MergerOptions<Schema>' requires 1 type argument(s).

22   options: MergerOptions
              ~~~~~~~~~~~~~

src/types/json-schema-merge-allof/index.d.ts:26:41 - error TS2314: Generic type 'PropertiesMerger' requires 1 type argument(s).

26   properties?: CombinedResolver<Schema, PropertiesMerger>
                                           ~~~~~~~~~~~~~~~~

src/types/json-schema-merge-allof/index.d.ts:27:36 - error TS2314: Generic type 'ItemsMerger' requires 1 type argument(s).

27   items?: CombinedResolver<Schema, ItemsMerger>
                                      ~~~~~~~~~~~

src/types/json-schema-merge-allof/index.d.ts:48:1 - error TS2309: An export assignment cannot be used in a module with other exported elements.

48 export = merger
   ~~~~~~~~~~~~~~~
$ eslint .

src/types/json-schema-merge-allof/index.d.ts
  1:49  error  Don't use `object` as a type. The `object` type is currently hard to use ([see this issue](https://github.com/microsoft/TypeScript/issues/21732)).
Consider using `Record<string, unknown>` instead, as it allows you to more easily inspect and use the keys  @typescript-eslint/ban-types

✖ 1 problem (1 error, 0 warnings)

TypeScript version: 4.1.3

I changed typings a bit so now it compiles and works, but not sure whether I did it right, especially given that I don't use any resolvers. Please take a look:

src/types/json-schema-merge-allof/index.d.ts
// Remove after https://github.com/mokkabonna/json-schema-merge-allof/pull/20 is merged and released

declare module "json-schema-merge-allof" {
  type notUndefined = boolean | number | string | Record<string, unknown> | null | notUndefined[]
  export type Resolver<Schema> = (
    values: Schema[],
    path: string[],
    mergeSchemas: (schemas: Schema[]) => Schema,
    options: MergerOptions<Schema>
  ) => notUndefined

  type PropertiesMerger<Schema> = {
    properties: Resolver<Schema>
    patternProperties: Resolver<Schema>
    additionalProperties: Resolver<Schema>
  }

  type ItemsMerger<Schema> = {
    items: Resolver<Schema>
    additionalItems: Resolver<Schema>
  }

  export type CombinedResolver<Schema, Mergers> = (
    values: Schema[],
    path: string[],
    mergers: Mergers,
    options: MergerOptions<Schema>
  ) => notUndefined

  export interface Resolvers<Schema> {
    [key: string]: Resolver<Schema>
    properties?: Resolver<Schema>
    items?: Resolver<Schema>
    defaultResolver?: Resolver<Schema>
  }

  export interface MergerOptions<Schema> {
    /**
     * @default false
     */
    ignoreAdditionalProperties?: boolean
    resolvers?: Resolvers<Schema>
    /**
     * @default true
     */
    deep?: boolean
  }

  export default function merger<Schema> (
    rootSchema: Schema,
    options?: MergerOptions<Schema>
  ): Schema
}

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

Successfully merging this pull request may close these issues.

Porting to typescript
3 participants