Skip to content

Commit 54b6010

Browse files
Recommend exporting $lib when writing a library (microsoft#2525)
fix microsoft#2519
1 parent 003d066 commit 54b6010

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

docs/extending-typespec/basics.md

+12-6
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,23 @@ This will create `tsconfig.json`. But we need to make a couple changes to this.
8585

8686
### 4. Create `lib.ts`
8787

88-
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:
88+
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`.
89+
90+
:::warn
91+
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.
92+
:::
93+
94+
The following shows an example:
8995

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

93-
export const myLibrary = createTypeSpecLibrary({
99+
export const $lib = createTypeSpecLibrary({
94100
name: "myLibrary",
95101
diagnostics: {},
96-
});
102+
} as const);
97103

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

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

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

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

113119
### 6. Build TypeScript

0 commit comments

Comments
 (0)