snowplow-javascript-tracker icon indicating copy to clipboard operation
snowplow-javascript-tracker copied to clipboard

Improve `SelfDescribingJson` type

Open greg-el opened this issue 1 year ago • 0 comments

The SelfDescribingJson type in core takes a type parameter, but will not accept a type with optional properties:

type Foo = {
  bar: string;
  baz?: string;
};

const sdj: SelfDescribingJson<Foo> = {
  schema: 'iglu:com.snowplow/foo/jsonschema/1-0-0',
  data: {
    bar: 'bar',
    // note baz not provided
  },
};

will fail with:

Type 'Foo' does not satisfy the constraint 'Record<keyof Foo, unknown>'.   Property 'baz' is optional in type 'Foo' but required in type 'Record<keyof Foo, unknown>'.ts(2344)

Schemas with optional properties are very common, and providing a type to accomodate this would improve type safety wherever SelfDescribingJson is currently used.

greg-el avatar Jun 12 '23 14:06 greg-el