# TypeScript library

Source: https://palantir.github.io/osdk-ts/frameworks/typescript-library

> Scaffold a standalone TypeScript package with the create-app typescript-library template, and add OSDK code that apps can share.

{/* cspell:ignore Ultracite ultracite oxlint oxfmt */}

The `typescript-library` template of `@osdk/create-app` creates a TypeScript package that builds ESM and type declarations with `tsc`, tests with Vitest, and lints with Ultracite. Use it to start a package of shared code, such as Ontology helpers that several of your OSDK apps import.

> [!NOTE]
> This template is beta and hidden from the CLI's framework list. Select it with `--template typescript-library`. It contains no OSDK code or dependencies; you add those yourself.

## When to use it

| You want to | Use |
| --- | --- |
| Share functions, types, or object-set helpers between several apps | This template |
| Build a browser app | The `react`, `vue`, or `expo` template. See [Quickstart](https://palantir.github.io/osdk-ts/quickstart.md). |
| Run a script, server, or serverless function | A plain Node project. See [Node.js](https://palantir.github.io/osdk-ts/frameworks/node.md). |

## Create the library

Run `@osdk/create-app` with the template, and pass `--skipOsdk` to skip the SDK prompts. `--skipOsdk` only works with an explicit `--sdkVersion 2.x`:

```npm
npx @osdk/create-app@latest my-library --template typescript-library --sdkVersion 2.x --skipOsdk
```

The CLI still asks for a Foundry URL, application RID, client ID, application URL, CORS proxy, and scopes, and writes `.env.development`, `.env.production`, `.npmrc`, and `foundry.config.json` from them. The library does not read the `.env` files or `foundry.config.json`; delete them if you do not need them. Keep `.npmrc` if you will install your SDK from the Foundry registry. It holds only the token line, so add your SDK's `@scope:registry=` line as described in [Installation](https://palantir.github.io/osdk-ts/installation.md).

Then install and build:

```sh
cd my-library
npm install
npm run build
```

## Project structure

```text
AGENTS.md
oxfmt.config.ts
oxlint.config.ts
package.json
tsconfig.json
.claude/
  CLAUDE.md
  settings.json
.vscode/
  extensions.json
src/
  index.ts
  __tests__/
    index.test.ts
```

| File | Purpose |
| --- | --- |
| `src/index.ts` | The package entry point. Everything it exports is the library's public API. The template exports a sample `greet` function. |
| `src/__tests__/` | Vitest tests. `tsconfig.json` excludes them from the build. |
| `package.json` | An ESM package (`"type": "module"`) whose `exports`, `main`, and `types` point at `dist/`. Only `dist/` is published. |
| `tsconfig.json` | Strict settings that compile `src/` to `dist/` with declaration files, declaration maps, and source maps. Targets ES2022. |
| `oxlint.config.ts`, `oxfmt.config.ts` | The Ultracite presets for oxlint and oxfmt. Lint warns on function expressions and prefers named function declarations. |
| `AGENTS.md`, `.claude/` | Coding standards for AI agents. `.claude/settings.json` runs `npx ultracite fix` after every file edit Claude Code makes. |
| `.vscode/extensions.json` | Recommends the Oxc extension. |

## Scripts

| Script | Runs | Does |
| --- | --- | --- |
| `build` | `tsc` | Type-checks and emits JavaScript and `.d.ts` files to `dist/`. |
| `clean` | `rm -rf dist` | Deletes the build output. |
| `test` | `vitest run` | Runs the tests once. |
| `test:watch` | `vitest` | Runs the tests in watch mode. |
| `lint` | `ultracite check` | Checks formatting and lint rules. |
| `fix` | `ultracite fix` | Fixes formatting and lint issues. |
| `format` | `oxfmt .` | Formats every file. |

The generated `README.md` mentions `npm run check`. The script is named `lint`.

## Add OSDK code

Install `@osdk/client` and your SDK. Your SDK comes from your Foundry npm registry, so set up `.npmrc` first as described in [Installation](https://palantir.github.io/osdk-ts/installation.md):

```npm
npm install --save-dev @osdk/client @my-app/sdk
```

Also list both in `peerDependencies`, so an app that installs your library provides them. The app and your library then share one `@osdk/client` and one copy of the SDK. Your generated SDK already declares `@osdk/client` as a peer.

Take the `Client` as a parameter instead of creating one in the library. The app keeps a single client with its own authentication, and your functions use it:

```ts title="src/engineers.ts"
import { Employee } from "@my-app/sdk";
import type { Client, Osdk } from "@osdk/client";

export async function listEngineers(
  client: Client,
): Promise<Osdk.Instance<Employee>[]> {
  const engineers: Osdk.Instance<Employee>[] = [];
  for await (const employee of client(Employee)
    .where({ department: "Engineering" })
    .asyncIter()) {
    engineers.push(employee);
  }
  return engineers;
}
```

Export it from the entry point:

```ts title="src/index.ts"
export { listEngineers } from "./engineers.js";
```

`tsc` does not rewrite import paths. Write relative imports with the `.js` extension, as above, so `dist/` also loads in Node.js without a bundler. [Create a client](https://palantir.github.io/osdk-ts/client.md#use-the-client-without-react) lists the client methods you can call.