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
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
You can use the following templates:
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
You can skip this step if you’ve used one of the templates above.
a. Initialize your package directory & package.json
Run the following commands:
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:
b. Install TypeSpec dependencies
Run the following command:
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
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.
d. Install and initialize TypeScript
Run the following commands:
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:
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:
Diagnostics are used for linters and decorators, which are covered in subsequent topics.
f. Create index.ts
Open ./src/index.ts
and import your library definition:
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:
You can then run npm run build
or npm run watch
to build or watch your library.
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.
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:
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
Step 4: Testing your TypeSpec library
TypeSpec provides a testing framework to assist in testing libraries. The examples here are shown using Node.js’s built-in test framework (available in Node 20+), but any other JS test framework can be used that will provide more advanced features like vitest, which is used in this project.
a. Add devDependencies
Ensure that you have the following in your package.json
:
Also add a vitest.config.ts
file at the root of your project.
b. Define the testing library
The first step is to define how your library can be loaded from the test framework. This will allow your library to be reused by other library tests.
- Create a new file
./src/testing/index.ts
with the following content
- Add an
exports
for thetesting
endpoint topackage.json
(update with correct paths)
c. Define the test host and test runner for your library
Define some of the test framework base pieces that will be used in the tests. There are 2 functions:
createTestHost
: This is a lower-level API that provides a virtual file system.createTestRunner
: This is a wrapper on top of the test host that will automatically add amain.tsp
file and automatically import libraries.
Create a new file test/test-host.js
(change test
to be your test folder)
d. Write tests
After setting up that infrastructure you can start writing tests. By default Node.js will run all files matching these patterns:
e. @test
decorator
The @test
decorator is a decorator loaded in the test environment. It can be used to collect any decorable type.
When using the compile
method it will return a Record<string, Type>
which is a map of all the types annotated with the @test
decorator.
f. Install vscode extension for the test framework
If you are using VSCode, you can install the Node test runner to run your tests from the editor. This will also allow you to easily debug your tests.
After installing the extension, you should be able to discover, run, and debug your tests from the test explorer.
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
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:
Next, they can import it into their TypeSpec program and use the namespace (if desired):
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.