Skip to content

Commit

Permalink
Recommend exporting $lib when writing a library (#2525)
Browse files Browse the repository at this point in the history
fix #2519
  • Loading branch information
timotheeguerin authored Oct 2, 2023
1 parent 003d066 commit 54b6010
Showing 1 changed file with 12 additions and 6 deletions.
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

0 comments on commit 54b6010

Please sign in to comment.