Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TS types for creating and updating records with attachment fields #358

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions src/attachment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,12 @@ export interface Attachment {
full: Thumbnail;
};
}

export interface AttachmentReference extends Partial<Attachment> {
id: Attachment['id'];
}

export interface CreateAttachment {
url: Attachment['url'];
filename?: Attachment['filename'];
}
4 changes: 2 additions & 2 deletions src/field_set.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Collaborator} from './collaborator';
import {Attachment} from './attachment';
import {Attachment, AttachmentReference, CreateAttachment} from './attachment';

export interface FieldSet {
[key: string]:
Expand All @@ -10,5 +10,5 @@ export interface FieldSet {
| Collaborator
| ReadonlyArray<Collaborator>
| ReadonlyArray<string>
| ReadonlyArray<Attachment>;
| ReadonlyArray<Attachment | CreateAttachment | AttachmentReference>;
Copy link
Author

@okovpashko okovpashko Mar 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only change I'm not sure about.

Without it, we get TS errors when passing fields with type eitherAttachmentReference or CreateAttachment to the create() or update() methods because the result of PartialFields has both fields' types and the indexed type from FieldSet:

interface MyFields extends FieldSet {
  name: string;
  files: Attachment[]
}

type PartialMyFields = PartialFields<MyFields>;

/*
The result type of PartialMyFields is
{
  [x: string]:
      | undefined
      | string
      | number
      | boolean
      | Collaborator
      | ReadonlyArray<Collaborator>
      | ReadonlyArray<string>
      | ReadonlyArray<Attachment | CreateAttachment | AttachmentReference>;
  name: string;
  files: (AttachmentReference | CreateAttachment)[]
}
*/

I will appreciate suggestions for a better solution.

}
29 changes: 18 additions & 11 deletions src/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,20 @@ import Base from './base';
import {Records} from './records';
import {FieldSet} from './field_set';
import {RecordData} from './record_data';
import {Attachment, AttachmentReference, CreateAttachment} from './attachment';

/* eslint-disable @typescript-eslint/no-explicit-any */
type TableError = any;
/* eslint-enable @typescript-eslint/no-explicit-any */

type CreateRecord<TFields> = Pick<RecordData<Partial<TFields>>, 'fields'>;
type CreateRecords<TFields> = string[] | Partial<TFields>[] | CreateRecord<TFields>[];
type PartialFields<TFields> = {
[Prop in keyof TFields]?: TFields[Prop] extends Attachment[]
? (CreateAttachment | AttachmentReference)[]
: TFields[Prop];
}

type CreateRecord<TFields> = Pick<RecordData<PartialFields<TFields>>, 'fields'>;
type CreateRecords<TFields> = string[] | PartialFields<TFields>[] | CreateRecord<TFields>[];

type OptionalParameters = {
typecast?: boolean;
Expand Down Expand Up @@ -51,7 +58,7 @@ interface TableSelectRecord<TFields extends FieldSet> {
(params?: QueryParams<TFields>): Query<TFields>;
}

interface TableCreateRecords<TFields extends FieldSet> {
interface TableCreateRecords<TFields extends PartialFields<FieldSet>> {
(recordsData: CreateRecords<TFields>, optionalParameters?: OptionalParameters): Promise<
Records<TFields>
>;
Expand All @@ -61,37 +68,37 @@ interface TableCreateRecords<TFields extends FieldSet> {
done: RecordCollectionCallback<TFields>
): void;
(recordsData: CreateRecords<TFields>, done: RecordCollectionCallback<TFields>): void;
(recordData: string | Partial<TFields>, optionalParameters?: OptionalParameters): Promise<
(recordData: string | PartialFields<TFields>, optionalParameters?: OptionalParameters): Promise<
Record<TFields>
>;
(
recordData: string | Partial<TFields>,
recordData: string | PartialFields<TFields>,
optionalParameters: OptionalParameters,
done: RecordCallback<TFields>
): void;
(recordData: string | Partial<TFields>, done: RecordCallback<TFields>): void;
(recordData: string | PartialFields<TFields>, done: RecordCallback<TFields>): void;
}

interface TableChangeRecords<TFields extends FieldSet> {
(recordId: string, recordData: Partial<TFields>, opts?: OptionalParameters): Promise<
(recordId: string, recordData: PartialFields<TFields>, opts?: OptionalParameters): Promise<
Record<TFields>
>;
(
recordId: string,
recordData: Partial<TFields>,
recordData: PartialFields<TFields>,
opts: OptionalParameters,
done: RecordCallback<TFields>
): void;
(recordId: string, recordData: Partial<TFields>, done: RecordCallback<TFields>): void;
(recordsData: RecordData<Partial<TFields>>[], opts?: OptionalParameters): Promise<
(recordId: string, recordData: PartialFields<TFields>, done: RecordCallback<TFields>): void;
(recordsData: RecordData<PartialFields<TFields>>[], opts?: OptionalParameters): Promise<
Records<TFields>
>;
(
recordsData: RecordData<Partial<TFields>>[],
opts: OptionalParameters,
done: RecordCollectionCallback<TFields>
): void;
(recordsData: RecordData<Partial<TFields>>[], done: RecordCollectionCallback<TFields>): void;
(recordsData: RecordData<PartialFields<TFields>>[], done: RecordCollectionCallback<TFields>): void;
}

interface TableDestroyRecords<TFields extends FieldSet> {
Expand Down