typesense / typesense/typesense-js

Generic type for documents improvement

Open
#152 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
569
Forks
99
Avg merge
5h 6m
Merged PRs (30d)
2

Description

Generic type for Documents can be enhanced. I propose the following change to DocumentSchema:

// schema must include at least an `id`
export type DocumentSchema = { id: string; [name: string]: any }

// if property can be undefined also make it nullable
type CreatableDocumentSchema<TDocument extends DocumentSchema> = {
  [name in keyof TDocument]: undefined extends Extract<TDocument[name], undefined>
    ? TDocument[name] | null
    : TDocument[name]
}

// if property can be undefined also make it nullable, and make everything else possibly undefined as well
// except `id`
type UpdatableDocumentSchema<TDocument extends DocumentSchema> = Pick<TDocument, 'id'> & {
  [name in keyof Omit<TDocument, 'id'>]?: undefined extends Extract<TDocument[name], undefined>
    ? TDocument[name] | null
    : TDocument[name]
}

I propose to extend ImportResponseFail to include the type of the document:

export interface ImportResponseFail<TDocument extends DocumentSchema> {
  success: false
  error: string
  document: TDocument
  code: number
}

With these modifications we can now be more specific for instance in import:

async import<TOptions extends DocumentImportParameters>(
    documents: (TOptions['action'] extends 'update' | 'emplace'
      ? UpdatableDocumentSchema<T>
      : CreatableDocumentSchema<T>)[],
    options?: TOptions
  ): Promise<ImportResponse<T>[]>

I'd also like to propose a type generator based on collection fields schema if this gets approved later.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating DocumentSchema, CreatableDocumentSchema, UpdatableDocumentSchema, ImportResponseFail, and the generic import method in the TypeScript client. Review the existing document import types and collection fields schema before assessing the proposed constraints. Done means the document and import response types reflect the proposal and the client’s type checks or tests pass.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.