mike-north / jsonapi-typescript

TypeScript type information for JSON:API documents
52 stars 5 forks source link

`PrimaryData` should include `null` #180

Open earthlyreason opened 4 years ago

earthlyreason commented 4 years ago

The JSON:API spec says:

Primary data MUST be either:

  • a single resource object, a single resource identifier object, or null, for requests that target single resources
  • an array of resource objects, an array of resource identifier objects, or an empty array ([]), for requests that target resource collections

Note "or null" in the first condition.

So the following code should typecheck:

export const doc: JSONAPI.Document = { data: null };

Instead, it gives the following error:

Type 'null' is not assignable to type 'PrimaryData<string, AttributesObject<{ [k: string]: Value; }>>'. [2322]

This is because the PrimaryData type, which is used as the type of data in DocWithData is an interface that assumes the value is an existing object.

Is this intentional?

Expected behavior The above should typecheck, but it gives the indicated error.

System Information (please complete the following information):