Skip to content

Commit 902a20a

Browse files
author
gernsdorfer
committed
feature optional Attributes
support optional Attributes
1 parent 3701ba7 commit 902a20a

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

index.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ export interface DocBase {
4040
export type Document = DocWithErrors | DocWithMeta | DocWithData;
4141
export type SingleResourceDoc<
4242
T extends string = string,
43-
A extends { [k: string]: JSON.Value } = { [k: string]: JSON.Value }
43+
A extends Attributes = Attributes
4444
> = DocWithData<ResourceObject<T, A>>;
4545
export type CollectionResourceDoc<
4646
T extends string = string,
47-
A extends { [k: string]: JSON.Value } = { [k: string]: JSON.Value }
47+
A extends Attributes = Attributes
4848
> = DocWithData<Array<ResourceObject<T, A>>>;
4949

5050
// an object describing the server’s implementation
@@ -53,7 +53,13 @@ export interface ImplementationInfo {
5353
meta?: MetaObject;
5454
}
5555

56-
export type Link = string | { href: string; meta?: MetaObject };
56+
interface Attribute {
57+
[k: string]: JSON.Value ;
58+
}
59+
60+
type Attributes = Partial<Attribute>;
61+
62+
type Link = string | { href: string; meta?: MetaObject };
5763

5864
// The top-level links object MAY contain the following members:
5965
export interface Links {
@@ -136,7 +142,7 @@ export interface RelationshipsObject {
136142
}
137143

138144
export type AttributesObject<
139-
ATTRS extends { [k: string]: JSON.Value } = { [k: string]: JSON.Value }
145+
ATTRS extends Attributes = Attributes
140146
> = { [K in keyof ATTRS]: ATTRS[K] };
141147

142148
export type Errors = ErrorObject[];

type-tests/toplevel-single.test.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,36 @@ const f: JSONAPI.SingleResourceDoc<
2525
type: 'book',
2626
attributes: {
2727
title: 'Great Expectations',
28-
chapters: 12,
29-
author: 'F. Scott Fitzgerald'
28+
author: 'F. Scott Fitzgerald',
29+
chapters: 12
3030
}
3131
}
3232
};
3333

34+
interface OptionalAttributes extends JSONAPI.AttributesObject {
35+
title: string;
36+
author?: string;
37+
chapters: number
38+
}
39+
40+
function optionalFunc(
41+
optionalAttributes: OptionalAttributes
42+
): JSONAPI.SingleResourceDoc<
43+
'book', OptionalAttributes
44+
> {
45+
return {
46+
data: {
47+
type: 'book',
48+
attributes: {
49+
title: optionalAttributes.title,
50+
author: optionalAttributes.author,
51+
chapters: optionalAttributes.chapters
52+
}
53+
}
54+
}
55+
}
56+
optionalFunc({title: 'Great Expectations', chapters:12})
57+
3458
const g = {
3559
data: {
3660
type: 'book',

0 commit comments

Comments
 (0)