Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ export interface DocBase {
export type Document = DocWithErrors | DocWithMeta | DocWithData;
export type SingleResourceDoc<
T extends string = string,
A extends { [k: string]: JSON.Value } = { [k: string]: JSON.Value }
A extends Attributes = Attributes
> = DocWithData<ResourceObject<T, A>>;
export type CollectionResourceDoc<
T extends string = string,
A extends { [k: string]: JSON.Value } = { [k: string]: JSON.Value }
A extends Attributes = Attributes
> = DocWithData<Array<ResourceObject<T, A>>>;

// an object describing the server’s implementation
Expand All @@ -53,6 +53,12 @@ export interface ImplementationInfo {
meta?: MetaObject;
}

interface Attribute {
[k: string]: JSON.Value ;
}

type Attributes = Partial<Attribute>;

export type Link = string | { href: string; meta?: MetaObject };

// The top-level links object MAY contain the following members:
Expand Down Expand Up @@ -136,7 +142,7 @@ export interface RelationshipsObject {
}

export type AttributesObject<
ATTRS extends { [k: string]: JSON.Value } = { [k: string]: JSON.Value }
ATTRS extends Attributes = Attributes
> = { [K in keyof ATTRS]: ATTRS[K] };

export type Errors = ErrorObject[];
28 changes: 26 additions & 2 deletions type-tests/toplevel-single.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,36 @@ const f: JSONAPI.SingleResourceDoc<
type: 'book',
attributes: {
title: 'Great Expectations',
chapters: 12,
author: 'F. Scott Fitzgerald'
author: 'F. Scott Fitzgerald',
chapters: 12
}
}
};

interface OptionalAttributes extends JSONAPI.AttributesObject {
title: string;
author?: string;
chapters: number
}

function optionalFunc(
optionalAttributes: OptionalAttributes
): JSONAPI.SingleResourceDoc<
'book', OptionalAttributes
> {
return {
data: {
type: 'book',
attributes: {
title: optionalAttributes.title,
author: optionalAttributes.author,
chapters: optionalAttributes.chapters
}
}
}
}
optionalFunc({title: 'Great Expectations', chapters:12})

const g = {
data: {
type: 'book',
Expand Down