# odoo-typescript

*Type declarations for Odoo.*

With `odoo-typescript` you can use your favorite bundler to build your Odoo
modules in TypeScript.

**Advantages**

- Add dependencies with a single `npm install`
- Out-of-the-box auto-completion for all Odoo modules
- Full type inference for your own modules via `define` / `definePatch`

**Disadvantages**

- An extra build step

## Installation

```bash
npm install odoo-typescript
```

Import the sub-package matching your Odoo version:

```ts
import * as ots from 'odoo-typescript/20.0'
```

Supported versions: `16.0`, `17.0`, `18.0`, `19.0`, `20.0`.

## API

Besides the type declarations, `odoo-typescript` exposes three runtime helpers.

### require

Import a module that is already loaded by Odoo.

```ts
import * as ots from 'odoo-typescript/20.0'

const owl = ots.require('@odoo/owl')
```

### define

Define a module. Returns a `Promise` that resolves with the factory's exports.

```ts
import * as ots from 'odoo-typescript/20.0'
import type { ORM } from 'odoo-typescript/20.0/dist/addons/web/core/orm_plugin'

const foo = await ots.define({
    name: '@my_module/foo',
    deps: {
        hooks: '@web/core/utils/hooks',
        owl: '@odoo/owl',
    },
    factory({ hooks, owl }) {
        const orm: ORM = hooks.useService('orm')

        class MyComponent extends owl.Component {
            // custom owl component
        }

        return { MyComponent }
    },
})
```

### definePatch

Patch an object or class from an existing module. Returns a `Promise` that
resolves once the patch has been applied.

```ts
import * as ots from 'odoo-typescript/20.0'

await ots.definePatch({
    deps: { navbar: '@point_of_sale/app/components/navbar/navbar' },
    objToPatch({ navbar }) {
        return navbar.Navbar.prototype
    },
    patch() {
        return {
            setup() {
                super.setup()
                console.log('Patching Navbar ...')
            },
        }
    },
})
```

> `definePatch` accepts an optional `name`. When omitted, a unique name is
> generated automatically.

## Type inference for custom modules

To enable auto-completion for your own modules, extend the `Modules` interface:

```ts
import * as ots from 'odoo-typescript/20.0'

const foo = ots.define({
    name: '@mymodule/foo',
    deps: {},
    factory() {
        // ...
    },
})

declare module 'odoo-typescript/20.0' {
    export interface Modules {
        '@mymodule/foo': Awaited<typeof foo>
    }
}
```

You can also extract the instance type of an exported class with the `Import`
helper:

```ts
import type { Import } from 'odoo-typescript/20.0'

type Navbar = Import<'@point_of_sale/app/components/navbar/navbar', 'Navbar'>
```

