TypeSpec

Describe your data up front and generate schemas, API specifications, clien...

README

TypeSpec


TypeSpec is a language for defining cloud service APIs and shapes. TypeSpec is a highly extensible language with primitives that can describe API shapes common among REST, OpenAPI, gRPC, and other protocols.

TypeSpec is excellent for generating many different API description formats, client and service code, documentation, and many other assets. All this while keeping your TypeSpec definition as a single source of truth.

Using TypeSpec, you can create reusable patterns for all aspects of an API and package those reusable patterns into libraries. These patterns establish "guardrails" for API designers and makes it easier to follow best practices than to deviate from them. TypeSpec also has a rich linter framework with the ability to flag anti-patterns as well as an emitter framework that lets you control of the output to ensure it follows the patterns you want.


  1. ```
  2. npm install -g @typespec/compiler
  3. ```

Tools


The TypeSpec VS Code extension can be installed from the VS Code marketplace or directly on the command line:

  1. ```
  2. tsp code install
  3. ```

The TypeSpec VS Extension can be installed from the VS Marketplace or directly on the command line:

  1. ```
  2. tsp vs install
  3. ```


TypeSpec to OpenAPI 3.0 Example


This example uses the @typespec/http, @typespec/rest, and @typespec/openapi3 libraries to define a basic REST service and generate an OpenAPI 3.0 document from it.

Run the following command and select "Generic REST API":

  1. ```
  2. tsp init
  3. ```

Hit enter a few times to confirm the defaults.

Copy the contents below into your main.tsp:

  1. ```typespec
  2. import "@typespec/http";
  3. import "@typespec/rest";
  4. import "@typespec/openapi3";

  5. using TypeSpec.Http;
  6. using TypeSpec.Rest;

  7. /** This is a pet store service. */
  8. @service({
  9.   title: "Pet Store Service",
  10. })
  11. @server("https://example.com", "The service endpoint")
  12. namespace PetStore;

  13. @route("/pets")
  14. interface Pets {
  15.   list(): Pet[];
  16. }

  17. model Pet {
  18.   @minLength(100)
  19.   name: string;

  20.   @minValue(0)
  21.   @maxValue(100)
  22.   age: int32;

  23.   kind: "dog" | "cat" | "fish";
  24. }
  25. ```

Install the dependencies of main.tsp:

  1. ```
  2. tsp install
  3. ```

Compile it to OpenAPI 3.0:

  1. ```
  2. tsp compile main.tsp --emit @typespec/openapi3
  3. ```

You can find the emitted OpenAPI output in ./tsp-output/openapi.json.

Advanced Scenarios


Installing nightly version


On every commit to the main branch, packages with changes are automatically published to npm with the @next tag.
The packages section shows which version corresponds to thenext tag for each package.

To use a nightly version of the packages, go over each one of the packages in the package.json file and update it to either the latest published @next version or @latest, whichever is the newest. You can also use the tag latest or next instead of an explicit version.

After updating the package.json file you can run npm update --force. Force is required as there might be some incompatible version requirement.

Example

  1. ```json5
  2. // Stable setup
  3. "dependencies": {
  4.   "@typespec/compiler": "~0.30.0",
  5.   "@typespec/http": "~0.14.0",
  6.   "@typespec/rest": "~0.14.0",
  7.   "@typespec/openapi": "~0.9.0",
  8. }

  9. // Consume next version
  10. // In this example: compiler and openapi have changes but rest library has none
  11. "dependencies": {
  12.   "@typespec/compiler": "~0.31.0-dev.5",
  13.   "@typespec/http": "~0.14.0",
  14.   "@typespec/rest": "~0.14.0", // No changes to @typespec/rest library so need to stay the latest.
  15.   "@typespec/openapi": "~0.10.0-dev.2",
  16. }
  17. ```