Skip to content

Commit

Permalink
Fix #538
Browse files Browse the repository at this point in the history
  • Loading branch information
allevo committed Nov 2, 2023
1 parent 79be2dc commit 6e90e97
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
24 changes: 24 additions & 0 deletions packages/docs/pages/open-source/usage/typescript.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { Callout } from 'nextra-theme-docs'

## Usage

You may want to types your variables. If needed, you can do it like this:

```ts copy
import type { TypedDocument, Orama, Results, SearchParams } from '@orama/orama'
import { create, insert, search } from '@orama/orama'
Expand Down Expand Up @@ -35,6 +37,28 @@ const result: Results<MovieDocument> = await search(movieDB, searchParams)
const title = result.hits[0].document.title // well typed!
```

## Enrich type of documents

Orama schema considers only the properties that are indexed.
Anyway, the added documents can have other properties that are not indexed.
The following example shows how to enrich the type of the documents.

```ts copy
const movieSchema = {
title: 'string',
} as const
const db = await create({ schema: movieSchema })

interface Movie {
title: string,
year: number,
}

const r = await search<typeof db, Movie>(db, { term: '' })
const title = r.hits[0].document.title // well typed!
const year = r.hits[0].document.year // well typed!
```

## Configuration

Set `moduleResolution` in the `compilerOptions` in your `tsconfig.json` to be either `Node16` or `NodeNext`.
Expand Down
18 changes: 18 additions & 0 deletions packages/orama/tests/type/issue_538.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
// https://github.com/oramasearch/orama/issues/538
import { expectAssignable } from 'tsd'
import { create, search } from '../../src/index.ts'

const movieSchema = {
title: 'string',
} as const
const db = await create({ schema: movieSchema })

interface Movie {
title: string,
year: number,
}

const r = await search<typeof db, Movie>(db, { term: '' })
expectAssignable<string>(r.hits[0].document.title)
expectAssignable<number>(r.hits[0].document.year)

0 comments on commit 6e90e97

Please sign in to comment.