Creating a TypeSpec Library
A TypeSpec library is a package that includes TypeSpec types, decorators, emitters or linters. These libraries are npm packages with some additional TypeSpec-specific metadata and conventions. This guide will walk you through the process of creating a new TypeSpec library, adding types to it, and distributing it on the public npm registry. Further sections will delve into the specifics of creating decorators, emitters and linters.
While this guide assumes that youâll be using TypeScript to develop your library, you can skip the TypeScript-related steps if you prefer to use plain JavaScript.
Prerequisites
Section titled âPrerequisitesâYouâll need to have both Node and npm installed. If youâre planning to develop multiple libraries simultaneously, itâs recommended to set up a monorepo to simplify the development process. TypeSpec itself uses pnpm.
Setting up with templates
Section titled âSetting up with templatesâYou can use the following templates:
# Create a TypeSpec library (Decorators & Linters) with TypeScript enabled.tsp init --template library-ts
# Create a TypeSpec emitter with TypeScript enabled.tsp init --template emitter-ts
Standard package structure
Section titled âStandard package structureâHereâs a high-level overview of what a TypeSpec package typically contains. Each of these files will be explained in more detail in the following sections.
- dist/index.js - The main file for your Node library
- lib/main.tsp - The main file for your TypeSpec types (optional)
- src/index.ts - The main file for your Node library in TypeScript
- src/lib.ts - The file that defines your TypeSpec library
- package.json - Metadata about your TypeSpec package
Step 1: Initial setup
Section titled âStep 1: Initial setupâYou can skip this step if youâve used one of the templates above.
a. Initialize your package directory & package.json
Section titled âa. Initialize your package directory & package.jsonâRun the following commands:
mkdir myLibrarycd myLibrarynpm init
After completing the wizard, youâll have a package.json file that defines your TypeSpec library.
Unlike Node libraries which support CommonJS (cjs), TypeSpec libraries must be ECMAScript Modules. To specify this, open your package.json
and add the following top-level configuration key:
"type": "module"
b. Install TypeSpec dependencies
Section titled âb. Install TypeSpec dependenciesâRun the following command:
npm install --save-peer @typespec/compiler
You might need to install other dependencies from the TypeSpec standard library. For example, if you want to use the metadata found in @typespec/openapi
, youâll need to install that as well.
Refer to the dependency section for more information on defining your dependencies.
c. Define your main files
Section titled âc. Define your main filesâYour package.json needs to refer to two main files: your Node module main file, and your TypeSpec main. The Node module main file is specified by the "main"
key in your package.json file, and it defines the entry point for your library when itâs used as a Node library. This must reference a JS file. The TypeSpec main defines the entry point for your library when itâs used from a TypeSpec program, and it can reference either a JS file (when your library doesnât contain any TypeSpec types) or a TypeSpec file.
"main": "dist/src/index.js", "exports": { ".": { "typespec": "./lib/main.tsp" }, // Additional named export are possible "./experimental": { "typespec": "./lib/experimental.tsp" }, // Wildcard export as well "./lib/*": { "typespec": "./lib/*.tsp" } }
d. Install and initialize TypeScript
Section titled âd. Install and initialize TypeScriptâRun the following commands:
npm install -D typescriptnpx tsc --init --strict
This will create a tsconfig.json
file. Youâll need to make a few changes to this file. Open tsconfig.json
and set the following settings:
"module": "Node16", // This and next setting tells TypeScript to use the new ESM import system to resolve types."moduleResolution": "Node16","target": "es2019","rootDir": ".","outDir": "./dist","sourceMap": true,
e. Create lib.ts
Section titled âe. 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. Make sure to export the library definition as $lib
.
For example:
import { createTypeSpecLibrary } from "@typespec/compiler";
export const $lib = createTypeSpecLibrary({ name: "myLibrary", diagnostics: {},} as const);
// Optional but convenient, these are meant to be used locally in your library.export const { reportDiagnostic, createDiagnostic } = $lib;
Diagnostics are used for linters and decorators, which are covered in subsequent topics.
f. Create index.ts
Section titled âf. Create index.tsâOpen ./src/index.ts
and import your library definition:
// Re-export $lib so the compiler can access it and register your library correctly.export { $lib } from "./lib.js";
g. Build TypeScript
Section titled âg. Build TypeScriptâTypeSpec can only import JavaScript files, so any changes made to TypeScript sources need to be compiled before they are visible to TypeSpec. To do this, run npx tsc -p .
in your libraryâs root directory. If you want to re-run the TypeScript compiler whenever files are changed, you can run npx tsc -p . --watch
.
Alternatively, you can add these as scripts in your package.json
to make them easier to invoke. Consider adding the following:
"scripts": { "clean": "rimraf ./dist ./temp", "build": "tsc -p .", "watch": "tsc -p . --watch", "test": "node --test ./dist/test/**/*.test.js", // Node 22+ "test": "node --test ./dist/test/" // Node 18, 20 }
You can then run npm run build
or npm run watch
to build or watch your library.
h. Add your main TypeSpec file
Section titled âh. Add your main TypeSpec fileâOpen ./lib/main.tsp
and import your JS entrypoint. This ensures that when TypeSpec imports your library, the code to define the library is run. When we add decorators in later topics, this import will ensure those get exposed as well.
import "../dist/index.js";
Step 2: Adding TypeSpec types to your library
Section titled âStep 2: Adding TypeSpec types to your libraryâOpen ./lib/main.tsp
and add any types you want to be available when users import this library. Itâs strongly recommended to put these types in a namespace that corresponds with the library name. For example, your ./lib/main.tsp
file might look like:
import "../dist/index.js";
namespace MyLibrary;model Person { name: string; age: uint8;}
Step 3: Defining dependencies
Section titled âStep 3: Defining dependenciesâWhen defining dependencies in a TypeSpec library, follow these rules:
- Use
peerDependencies
for all TypeSpec libraries (and the compiler) that you use in your own library or emitter. - Use
devDependencies
for other TypeSpec libraries that are only used in tests. - Use
dependencies
ordevDependencies
for any other packages, depending on whether theyâre used in library code or in test/dev scripts.
TypeSpec libraries are defined using peerDependencies
to avoid having multiple versions of the compiler or library running at the same time.
Example
{ "dependencies": { "yaml": "~2.3.1", // This is a regular package this library/emitter will use }, "peerDependencies": { // These are all TypeSpec libraries this library/emitter depends on "@typespec/compiler": "~0.43.0", "@typespec/http": "~0.43.1", "@typespec/openapi": "~0.43.0", }, "devDependencies": { // This TypeSpec library is only used in the tests but is not required to use this library. "@typespec/versioning": "~0.43.0", // TypeScript is only used during development "typescript": "~5.0.2", },}
Step 4: Testing your TypeSpec library
Section titled âStep 4: Testing your TypeSpec libraryâTesting see documentation for adding tests to your library.
Step 5: Publishing your TypeSpec library
Section titled âStep 5: Publishing your TypeSpec libraryâTo publish your library to the public npm registry, follow the instructions in the npm documentation.
Step 6: Importing your TypeSpec library
Section titled âStep 6: Importing your TypeSpec libraryâOnce your TypeSpec library is published, users can install and use it just like any of the standard TypeSpec libraries. First, they need to install it:
npm install $packageName
Next, they can import it into their TypeSpec program and use the namespace (if desired):
import "MyLibrary";using MyLibrary;
model Employee extends Person { job: string;}
Step 7: Next steps
Section titled âStep 7: Next stepsâTypeSpec libraries can contain more than just types. For more details on how to write decorators, emitters and linters, refer to the subsequent topics.