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

Recommend exporting $lib when writing a library #2525

Merged
merged 1 commit into from
Oct 2, 2023
Merged
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
18 changes: 12 additions & 6 deletions docs/extending-typespec/basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,23 @@ This will create `tsconfig.json`. But we need to make a couple changes to this.

### 4. Create `lib.ts`

Open `./src/lib.ts` and create your library definition that registers your library with the TypeSpec compiler and defines any diagnostics your library will emit. The following shows an example:
Open `./src/lib.ts` and create your library definition that registers your library with the TypeSpec compiler and defines any diagnostics your library will emit. Make sure to export the library definition as `$lib`.

:::warn
If `$lib` is not accessible from your library package (`import {$lib} from "my-library";`) some functionality will be unavailable like validation of emitter options, linter rules, etc.
:::

The following shows an example:

```typescript
import { createTypeSpecLibrary } from "@typespec/compiler";

export const myLibrary = createTypeSpecLibrary({
export const $lib = createTypeSpecLibrary({
name: "myLibrary",
diagnostics: {},
});
} as const);

// optional but convenient
// Optional but convenient, those are meant to be used locally in your library.
export const { reportDiagnostic, createDiagnostic, createStateSymbol } = myLibrary;
```

Expand All @@ -105,9 +111,9 @@ Diagnostics are used for linters and decorators which are covered in subsequent

Open `./src/index.ts` and import your library definition:

<!-- prettier-ignore -->
```typescript
import { myLibrary } from "./lib.js";
// Re-export $lib to the compiler can get access to it and register your library correctly.
export { $lib } from "./lib.js";
```

### 6. Build TypeScript
Expand Down
Loading