diff --git a/packages/docs/pages/open-source/usage/typescript.mdx b/packages/docs/pages/open-source/usage/typescript.mdx index 459a8e64..85f3ee2a 100644 --- a/packages/docs/pages/open-source/usage/typescript.mdx +++ b/packages/docs/pages/open-source/usage/typescript.mdx @@ -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' @@ -35,6 +37,28 @@ const result: Results = 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(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`. diff --git a/packages/orama/tests/type/issue_538.test-d.ts b/packages/orama/tests/type/issue_538.test-d.ts new file mode 100644 index 00000000..18cbb3cd --- /dev/null +++ b/packages/orama/tests/type/issue_538.test-d.ts @@ -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(db, { term: '' }) +expectAssignable(r.hits[0].document.title) +expectAssignable(r.hits[0].document.year)