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

Add typescript documentation to describe types with examples #521

Merged
merged 1 commit into from
Oct 18, 2023
Merged
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
10 changes: 0 additions & 10 deletions packages/docs/pages/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,6 @@ Orama ships **ESM** modules by default. This allows us to move faster when provi

CommonJS imports are still supported, but we suggest you to migrate to ESM.

## TypeScript

Set `moduleResolution` in the `compilerOptions` in your `tsconfig.json` to be either `Node16` or `NodeNext`.

When importing types, always refer to the standard orama import:

```ts copy
import type { Language } from '@orama/orama'
```

# Community Rewards

![Orama Community Rewards](/misc/community-rewards.png)
Expand Down
46 changes: 46 additions & 0 deletions packages/docs/pages/usage/typescript.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Callout } from 'nextra-theme-docs'

# Use with Typescript

## Usage

```ts copy
import type { TypedDocument, Orama, Results, SearchParams } from '@orama/orama'
import { create, insert, search } from '@orama/orama'

const movieSchema = {
title: 'string',
year: 'number',
actors: 'string[]',
isFavorite: 'boolean',
stars: 'enum'
} as const // <-- this is important
type MovieDocument = TypedDocument<Orama<typeof movieSchema>>

const movieDB: Orama<typeof movieSchema> = await create({
schema: movieSchema,
})

const idP: string = await insert(movieDB, {
title: 'The Godfather',
year: 1972,
actors: ['Marlon Brando', 'Al Pacino'],
isFavorite: true,
})

const searchParams: SearchParams<Orama<typeof movieSchema>> = {
term: 'godfather',
}
const result: Results<MovieDocument> = await search(movieDB, searchParams)
const title = result.hits[0].document.title // well typed!
```

## Configuration

Set `moduleResolution` in the `compilerOptions` in your `tsconfig.json` to be either `Node16` or `NodeNext`.

When importing types, always refer to the standard orama import:

```ts copy
import type { Language } from '@orama/orama'
```
13 changes: 6 additions & 7 deletions packages/orama/tests/type/basic.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { expectType } from 'tsd'
import type { InternalTypedDocument, Orama, Results, Schema } from '../../src/types.d.ts'
import type { Orama, Results, SearchParams, TypedDocument } from '../../src/types.d.ts'
import { create, insert, search } from '../../src/index.js'

const movieSchema = {
Expand All @@ -10,8 +10,7 @@ const movieSchema = {
isFavorite: 'boolean',
stars: 'enum'
} as const
type MovieSchema = Schema<typeof movieSchema>
type MovieDocument = InternalTypedDocument<MovieSchema>
type MovieDocument = TypedDocument<Orama<typeof movieSchema>>

const movieDBP = create({
schema: movieSchema,
Expand All @@ -27,11 +26,11 @@ const idP = insert(movieDB, {
})
expectType<Promise<string>>(idP)

const resultP = search(movieDB, {
const searchParams: SearchParams<Orama<typeof movieSchema>> = {
term: 'godfather',
})
}

const resultP = search(movieDB, searchParams)
expectType<Promise<Results<MovieDocument>>>(resultP)
const result = await resultP
expectType<string>(result.hits[0].document.title)