-
Notifications
You must be signed in to change notification settings - Fork 6
Description
Is your feature request related to a problem? Please describe.
I would like to use the types in your library to describe the interfaces in our application to consume them in the backend and frontend libraries.
Currently, I have an issue with optional attributes that are not allowed by the index signature of the { [k: string]: JSON.Value } which is used by the SingleResourceDoc, CollectionResourceDoc and AttributesObject interfaces.
Following a simplified and JSON:API compliant use case:
Example response body 1:
{
"data": {
"id": "1",
"type": "_resource_type",
"attributes": {
"mandatory_attribute": "mandatory value"
}
}
}
Example response body 2:
{
"data": {
"id": "2",
"type": "_resource_type",
"attributes": {
"mandatory_attribute": "mandatory value",
"optional_property": "optional value",
}
}
}
Expected interface specification:
export type ResourceRequestBody = SingleResourceDoc<
'_resource_type_',
{
mandatory_property: string;
optional_property?: string;
}
>;
This scenario would lead to the following typescript error:
TS2344: Type '{ mandatory_property: string; optional_property?: string | undefined; }' does not satisfy the constraint '{ [k: string]: Value; }'.
Property 'optional_property' is incompatible with index signature.
Type 'string | undefined' is not assignable to type 'Value'.
Type 'undefined' is not assignable to type 'Value'.
As described in the JSON:API specification (json-schema), attributes has to be an object which may contain properties with a naming scheme which matches the pattern ^[a-zA-Z0-9](?:[-\\w]*[a-zA-Z0-9])?$ and are not named relationships, links, id or type.
"attributes": {
"description": "Members of the attributes object (\"attributes\") represent information about the resource object in which it's defined.",
"type": "object",
"patternProperties": {
"^[a-zA-Z0-9](?:[-\\w]*[a-zA-Z0-9])?$": {
"description": "Attributes may contain any valid JSON value."
}
},
"not": {
"anyOf": [
{"required": ["relationships"]},
{"required": ["links"]},
{"required": ["id"]},
{"required": ["type"]}
]
},
"additionalProperties": false
},
Describe the solution you'd like
I would appreciate the support for optional attributes or an example of how to use them. Sadly I am not aware of how to change the interface signature to support optional attributes so I am not able to provide a PR at the moment.
Describe alternatives you've considered
I tried to pass different specifications to the generic to support optional attributes but the index signature always wins as expected.
Additional context