TypeSpec
repository·main·Indexed 11 days ago
https://github.com/microsoft/typespecA highly extensible language for defining cloud service APIs and shapes. TypeSpec provides a single source of truth for API definitions used to generate OpenAPI, gRPC, documentation, and client/service code. It supports deployment via Docker and includes emitters for C#, Java, and TypeScript with built-in support for OAuth 2.0 authentication flows.
What's inside TypeSpec
- Spector is a tool and TypeSpec library designed to describe specifications for various protocols. It enables the running of mock servers and mock clients based on these specifications and provided mock API data.
What is TypeSpec?
mainTypeSpec is a modern, high-level API definition language designed for API-centric development. It allows you to describe APIs using a concise, familiar syntax (inspired by TypeScript and C#) and then compile those descriptions into various industry-standard formats such as OpenAPI, JSON Schema, or Protobuf—often simultaneously.
Key capabilities include:
- Abstraction: Encapsulate common data types, API patterns, and guidelines into reusable components.
- Interoperability: Output multiple protocols, clients, servers, and documentation from a single source of truth.
- Extensibility: Extend the language with custom decorator vocabularies and type templates to model business-specific logic.
- Ecosystem: Package common types, linters, and emitters as NPM packages for distribution within organizations or the wider community.
Overview of the Mutator Framework
mainThe Mutator Framework is an experimental utility for building mutations of the TypeSpec type graph. Mutations create a parallel type graph that contains modifications to the original graph and additional metadata relevant to consumers.
To use the framework, you typically:
- Create mutation classes to control how TypeSpec types (models, properties, unions, etc.) are traversed and transformed.
- Use helper APIs on mutation nodes to mutate types or traverse to related nodes.
- Instantiate a
MutationEngine(such asSimpleMutationEngine) using theTypekitfrom your TypeSpec program.
What is the Microsoft TypeSpec Generator
mainThe Microsoft TypeSpec Generator is a code generation library bundled with the@typespec/http-client-csharpemitter. Its primary purpose is to transform TypeSpec API definitions into strongly-typed C# client libraries designed for accessing RESTful web services. It features a modular and extensible architecture that allows for various output formats and customization options.Use @typespec/tspd to generate signatures and documentation
main@typespec/tspd is an experimental tool designed to assist in building TypeSpec libraries and emitters. It provides functionality to:
- Generate decorator signatures and type checks.
- Generate documentation for library types and emitter options.
Warning: This library is experimental and its API is subject to significant changes in future versions.
# Example usage for generating external signatures tspd --enable-experimental gen-extern-signature # Example usage for generating documentation for the current directory tspd --enable-experimental doc . --output-dir ./docs/Use @typespec/monarch for Monaco Editor tokenization
main@typespec/monarch provides a tokenizer for the Monarch language service, specifically designed for use within the monaco-editor. This allows the Monaco Editor to provide syntax highlighting for TypeSpec files.TypeSpec 1.0 GA Production-Ready Components
mainTypeSpec 1.0 is generally available and includes several production-ready components for API modeling and generation. Use these stable packages for production environments:
Compiler and Core Libraries
@typespec/compiler: The core TypeSpec language compiler.@typespec/http: Support for the HTTP protocol.@typespec/openapi: Support for OpenAPI definitions.
IDE Support
typespec-vscode: The official VS Code extension for TypeSpec development.
Stable Emitters
@typespec/openapi3: Generates OpenAPI 3.0 specifications.@typespec/json-schema: Generates JSON Schema outputs.
TypeSpec Language Support for Visual Studio
mainThe TypeSpec Visual Studio extension provides comprehensive language support for TypeSpec files within the Visual Studio IDE.
Key features include:
- Live diagnostic reporting: Real-time feedback on syntax and errors.
- IntelliSense: Syntax highlighting, code completion, and hover information.
- Navigation & Refactoring: Go to definition and rename refactoring.
- Editor Utilities: Code folding and formatting.
Use TypeSpec.Streams decorators and models
mainThe
@typespec/streamslibrary provides bindings for stream-based operations in TypeSpec. It includes the following core components:Decorators
@streamOf: Used to define stream-based properties or types.
Models
Stream: A data type representing a stream.
Use TMLanguage Generator for authoring TextMate grammars
mainThetmlanguage-generatorpackage is a helper library designed to simplify the process of authoring TextMate grammars using TypeScript. It provides abstractions that allow you to define grammar rules programmatically rather than writing raw JSON or YAML structures manually.Use @typespec/http-client-csharp for C# HTTP Client generation
mainThe
@typespec/http-client-csharplibrary is a TypeSpec library designed to emit HTTP Client libraries for C#. It works in conjunction withTypeSpec.HttpClientto transform TypeSpec definitions into functional C# client code.For detailed instructions on configuring the emitter and using generator plugins, refer to the specific documentation pages for Emitter usage and Generator plugins.
What is Visibility in TypeSpec
mainVisibility is a language feature used to define different "views" of a model across different operations or contexts. It allows you to control which properties of a model are included or excluded by an emitter depending on the context (e.g., during a
POSTrequest vs. aGETrequest).Key Concepts
- Applies to Model Properties: Visibility only affects properties within a model.
- Visibility Classes: A visibility class is an
enumthat defines the modifiers (flags) for a specific context. Anyenumcan serve as a visibility class. - Default Visibility: Every visibility class has a default set of modifiers applied to properties if no explicit visibility is set.
- Enum-based vs. String-based: You should use enum-based visibility (e.g.,
@visibility(Lifecycle.Read)) for new specifications. While string-based visibility (e.g.,@visibility("create")) is currently supported for backwards compatibility, it may be deprecated.