jsontypedef / json-typedef-js

A JavaScript / TypeScript implementation of JSON Type Definition
https://jsontypedef.com
MIT License
86 stars 3 forks source link

Export "Metadata" type so that consumers can extend it #49

Open joshmossas opened 1 year ago

joshmossas commented 1 year ago

Right now there is no way for consumers of this library to extend the metadata property in SharedFormProperties because SharedFormProperties isn't exported from the library.

Current Type Definition

// ./src/scheam.ts line 112
interface SharedFormProperties {
  definitions?: { [definition: string]: Schema };
  metadata?: { [name: string]: unknown };
  nullable?: boolean;
}

Proposed Change

export interface SharedFormProperties {
  definitions?: { [definition: string]: Schema };
  metadata?: SchemaMetadata;
  nullable?: boolean;
}

export interface SchemaMetadata {
  [key: string]: unknown;
}

Reasoning

In doing this tools and apps that consume this library can define type hints for metadata that they use similar to how people extend the request object in Fastify or Express. (See an example here: https://github.com/fastify/help/issues/122#issuecomment-915263438)

Here is an example of what this would allow. Writing a .d.ts file like the following.

// jtd.d.ts
import "jtd"
declare module "jtd" {
  export interface SchemaMetadata {
     id?: string;
     description?: string;
  }
}

Results in type hints like this: image

joshmossas commented 1 year ago

If the proposal looks good to you I'd be willing to make a PR to get this feature working.

I have a working fork here but it diverges in other places.