Skip to content

Improve SelfDescribingJson type #1203

@greg-el

Description

@greg-el

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type:enhancementNew features or improvements to existing features.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions