mokkabonna / json-schema-merge-allof

Simplify your schema by combining allOf
97 stars 23 forks source link

Add TypeScript type definitions #20

Open awlayton opened 4 years ago

awlayton commented 4 years ago

Sort of resolves #3

coveralls commented 4 years ago

Coverage Status

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

Envek commented 3 years ago

@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' requires 1 type argument(s). 6 options: MergerOptions ~~~~~~~~~~~~~ src/types/json-schema-merge-allof/index.d.ts:22:12 - error TS2314: Generic type 'MergerOptions' 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 ~~~~~~~~~~~~~~~~ src/types/json-schema-merge-allof/index.d.ts:27:36 - error TS2314: Generic type 'ItemsMerger' requires 1 type argument(s). 27 items?: CombinedResolver ~~~~~~~~~~~ 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` 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 ```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 | null | notUndefined[] export type Resolver = ( values: Schema[], path: string[], mergeSchemas: (schemas: Schema[]) => Schema, options: MergerOptions ) => notUndefined type PropertiesMerger = { properties: Resolver patternProperties: Resolver additionalProperties: Resolver } type ItemsMerger = { items: Resolver additionalItems: Resolver } export type CombinedResolver = ( values: Schema[], path: string[], mergers: Mergers, options: MergerOptions ) => notUndefined export interface Resolvers { [key: string]: Resolver properties?: Resolver items?: Resolver defaultResolver?: Resolver } export interface MergerOptions { /** * @default false */ ignoreAdditionalProperties?: boolean resolvers?: Resolvers /** * @default true */ deep?: boolean } export default function merger ( rootSchema: Schema, options?: MergerOptions ): Schema } ```