snowplow-javascript-tracker
snowplow-javascript-tracker copied to clipboard
Improve `SelfDescribingJson` type
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.