TypeSpec

repository·main·Indexed 11 days ago

https://github.com/microsoft/typespec

A 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.

Tokens
330.3K
Snippets
1.2K
Records
1.6K
Agent score
90%

What's inside TypeSpec

  1. What is TypeSpec?

    main

    TypeSpec 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.
  2. Overview of the Mutator Framework

    main

    The 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:

    1. Create mutation classes to control how TypeSpec types (models, properties, unions, etc.) are traversed and transformed.
    2. Use helper APIs on mutation nodes to mutate types or traverse to related nodes.
    3. Instantiate a MutationEngine (such as SimpleMutationEngine) using the Typekit from your TypeSpec program.
  3. What is the Microsoft TypeSpec Generator

    main
    The Microsoft TypeSpec Generator is a code generation library bundled with the @typespec/http-client-csharp emitter. 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.
  4. 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/
  5. TypeSpec 1.0 GA Production-Ready Components

    main

    TypeSpec 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.
  6. TypeSpec Language Support for Visual Studio

    main

    The 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.
  7. Use @typespec/http-client-csharp for C# HTTP Client generation

    main

    The @typespec/http-client-csharp library is a TypeSpec library designed to emit HTTP Client libraries for C#. It works in conjunction with TypeSpec.HttpClient to 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.

  8. What is Visibility in TypeSpec

    main

    Visibility 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 POST request vs. a GET request).

    Key Concepts

    • Applies to Model Properties: Visibility only affects properties within a model.
    • Visibility Classes: A visibility class is an enum that defines the modifiers (flags) for a specific context. Any enum can 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.