≪ Today I learned.
RSS購読
    公開日
    タグ
    TypeScript
    著者
    ダーシノ

    TypeSpecがDSLを選択した理由

    TypeSpecはAPI定義言語で、OpenAPIのスキーマ定義やクライアント、ドキュメントなどを生成できる。

    import "@typespec/http";
    
    using TypeSpec.Http;
    
    model Store {
      name: string;
      address: Address;
    }
    
    model Address {
      street: string;
      city: string;
    }
    
    @route("/stores")
    interface Stores {
      list(@query filter: string): Store[];
      read(@path id: Store): Store;
    }

    TypeSpecのサンプルコードを見た限りでは、TypeScriptのDecoratorを使えばいけるんじゃ?わざわざDSLにした理由は?と気になってTwitter(現X)を検索したところ、開発者がコメントしていた。

    Early iterations of TypeSpec were in fact a TypeScript-type-based DSL! From what I remember, it works great for describing JSON data shapes, but not so great for describing validations and non-JSON schemas, and didn’t work very well at all for describing HTTP-level semantics.

    — Brian Terlson (@bterlson) January 24, 2024

    データバリデーションなどを表現するには、TypeScriptではうまく表現できなかったらしい。