Skip to content

Commit

Permalink
Fix #538 (#541)
Browse files Browse the repository at this point in the history
  • Loading branch information
allevo committed Nov 2, 2023
1 parent 23337a8 commit 21c2a96
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
25 changes: 25 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,29 @@ 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,
}

// this is important ---v
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)

1 comment on commit 21c2a96

@vercel
Copy link

@vercel vercel bot commented on 21c2a96 Nov 2, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.