32
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

JSON-SchemaからTypeScriptの型定義を生成する

Last updated at Posted at 2019-07-27

REST APIの通信も型で守りたい!となった時に便利なライブラリがあったので紹介します。
json-schema-to-typeを使えばjson-schemaからTypeScriptの型定義を自動で生成することができます。

json-schema-to-type

使い方

コマンドラインの使い方です。
Qiita API v2のjson-schemaで試します。

これだけ。

curl https://qiita.com/api/v2/schema | npx json2ts > qiita-types.d.ts

一瞬で型定義が生成できます。

qiita-type.d.ts
/* tslint:disable */
/**
 * This file was automatically generated by json-schema-to-typescript.
 * DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
 * and run json-schema-to-typescript to regenerate this file.
 */

/**
 * In this schema file, we represents the public interface of Qiita API v2 in JSON Hyper Schema draft v4.
 */
export interface QiitaAPIV2JSONSchema {
  access_token: AccessToken;
  authenticated_user: AuthenticatedUser;
  comment: Comment;
  expanded_template: ExpandedTemplate;
  group: Group;
  item: Item;
  like: Like;
  project: Project;
  reaction: EmojiReaction;
  tag: Tag;
  tagging: Tagging;
  team: Team;
  team_invitation: InvitedMember;
  template: Template;
  user: User4;
  [k: string]: any;
}

...
32
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
32
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?