Skip to content

Files

Latest commit

 

History

History
623 lines (560 loc) · 9.06 KB

interface.en-US.md

File metadata and controls

623 lines (560 loc) · 9.06 KB

Interface

common.ts:

export interface AAA {
  other1: string;
}
export interface BBB {
  other10: string;
}

export enum Label {
  // 枚举
  LABEL_OPTIONAL = 1,
  LABEL_REQUIRED = 2,
  LABEL_REPEATED = 3,
}

export namespace Param {
  export namespace C {
    export namespace D {
      export namespace E {
        export namespace F {
          export interface GetBaseDetailResponse {
            id: string;
          }
        }
      }
    }
  }
}

export namespace Param_1 {
  export enum Label {
    // 枚举
    LABEL_OPTIONAL = 1,
    LABEL_REQUIRED = 2,
    LABEL_REPEATED = 3,
  }
  export interface A {
    name: string;
  }
}

1.1 Basic Types

interface Interface_1_1 {
  attr1: string;
  attr2: number;
  attr3?: boolean;
}

result:

{
  "additionalProperties": false,
  "properties": {
    "attr1": {
      "type": "string",
    },
    "attr2": {
      "type": "number",
    },
    "attr3": {
      "type": "boolean",
    },
  },
  "required": [
    "attr1",
    "attr2",
  ],
  "type": "object",
}

1.2 Union Types

Example:

import { AAA, BBB } from './common';

interface Interface_8 {
  attr: AAA | BBB;
}

result:

{
  "additionalProperties": false,
  "definitions": {
    "AAA": {
      "additionalProperties": false,
      "properties": {
        "other1": {
          "type": "string",
        },
      },
      "required": [
        "other1",
      ],
      "type": "object",
    },
    "BBB": {
      "additionalProperties": false,
      "properties": {
        "other10": {
          "type": "string",
        },
      },
      "required": [
        "other10",
      ],
      "type": "object",
    },
  },
  "properties": {
    "attr": {
      "anyOf": [
       {
          "$ref": "#/definitions/AAA",
        },
       {
          "$ref": "#/definitions/BBB",
        },
      ],
    },
  },
  "required": [
    "attr",
  ],
  "type": "object",
}

1.3 Intersection Types

import { AAA, BBB } from './common';

interface Interface_9 {
  attr: AAA & BBB;
}

result:

{
  "additionalProperties": false,
  "definitions": {
    "AAA": {
      "additionalProperties": false,
      "properties": {
        "other1": {
          "type": "string",
        },
      },
      "required": [
        "other1",
      ],
      "type": "object",
    },
    "BBB": {
      "additionalProperties": false,
      "properties": {
        "other10": {
          "type": "string",
        },
      },
      "required": [
        "other10",
      ],
      "type": "object",
    },
  },
  "properties": {
    "attr": {
      "allOf": [
       {
          "$ref": "#/definitions/AAA",
        },
       {
          "$ref": "#/definitions/BBB",
        },
      ],
    },
  },
  "required": [
    "attr",
  ],
  "type": "object",
}

1.4 Array Types

1.4.1 Basic Array Types

interface Interface_4 {
  attr: string[];
}

result:

{
  "additionalProperties": false,
  "properties": {
    "attr": {
      "items": {
        "type": "string",
      },
      "type": "array",
    },
  },
  "required": [
    "attr",
  ],
  "type": "object",
}

1.4.2 Complex Array Types

interface Interface_5 {
  attr: Array<string | number>;
}

interface Interface_6 {
  attr: (string | number)[];
}

result:

{
  "additionalProperties": false,
  "properties":  {
    "attr":  {
      "items":  {
        "anyOf": [
           {
            "type": "string",
          },
           {
            "type": "number",
          },
        ],
      },
      "type": "array",
    },
  },
  "required": [
    "attr",
  ],
  "type": "object",
}

1.5 Nesting

1.5.1 Nested Basic Types

import { AAA } from './common';

interface Interface_7 {
  attr: AAA;
}

result:

{
  "additionalProperties": false,
  "definitions": {
    "AAA": {
      "additionalProperties": false,
      "properties": {
        "other1": {
          "type": "string",
        },
      },
      "required": [
        "other1",
      ],
      "type": "object",
    },
  },
  "properties": {
    "attr": {
      "$ref": "#/definitions/AAA",
    },
  },
  "required": [
    "attr",
  ],
  "type": "object",
}

1.5.2 Nested Union Types

import { AAA, BBB } from './common';

interface Interface_8 {
  attr: AAA | BBB;
}

result:

{
  "additionalProperties": false,
  "definitions": {
    "AAA": {
      "additionalProperties": false,
      "properties": {
        "other1": {
          "type": "string",
        },
      },
      "required": [
        "other1",
      ],
      "type": "object",
    },
    "BBB": {
      "additionalProperties": false,
      "properties": {
        "other10": {
          "type": "string",
        },
      },
      "required": [
        "other10",
      ],
      "type": "object",
    },
  },
  "properties": {
    "attr": {
      "anyOf": [
       {
          "$ref": "#/definitions/AAA",
        },
       {
          "$ref": "#/definitions/BBB",
        },
      ],
    },
  },
  "required": [
    "attr",
  ],
  "type": "object",
}

1.5.3 Nested Intersection Types

import { AAA, BBB } from './common';

interface Interface_9 {
  attr: AAA & BBB;
}

result:

{
  "additionalProperties": false,
  "definitions": {
    "AAA": {
      "additionalProperties": false,
      "properties": {
        "other1": {
          "type": "string",
        },
      },
      "required": [
        "other1",
      ],
      "type": "object",
    },
    "BBB": {
      "additionalProperties": false,
      "properties": {
        "other10": {
          "type": "string",
        },
      },
      "required": [
        "other10",
      ],
      "type": "object",
    },
  },
  "properties": {
    "attr": {
      "allOf": [
       {
          "$ref": "#/definitions/AAA",
        },
       {
          "$ref": "#/definitions/BBB",
        },
      ],
    },
  },
  "required": [
    "attr",
  ],
  "type": "object",
}

1.5.4 Nested Intersection Array Types

import { AAA, BBB } from './common';

interface Interface_10 {
  attr: (AAA & BBB)[];
}

interface Interface_11 {
  attr: Array<AAA & BBB>;
}

result:

{
  "additionalProperties": false,
  "definitions": {
    "AAA": {
      "additionalProperties": false,
      "properties": {
        "other1": {
          "type": "string",
        },
      },
      "required": [
        "other1",
      ],
      "type": "object",
    },
    "BBB": {
      "additionalProperties": false,
      "properties": {
        "other10": {
          "type": "string",
        },
      },
      "required": [
        "other10",
      ],
      "type": "object",
    },
  },
  "properties": {
    "attr": {
      "items": {
        "allOf": [
         {
            "$ref": "#/definitions/AAA",
          },
         {
            "$ref": "#/definitions/BBB",
          },
        ],
      },
      "type": "array",
    },
  },
  "required": [
    "attr",
  ],
  "type": "object",
}

1.5.5 Nested loop

export interface Other_4 {
  name: string;
  age: number;
  children: Other_4;
}

result:

{
  "additionalProperties": false,
  "definitions": {
    "Other_4": {
      "additionalProperties": false,
      "properties": {
        "age": {
          "type": "number",
        },
        "children": {
          "$ref": "#/definitions/Other_4",
        },
        "name": {
          "type": "string",
        },
      },
      "required": [
        "name",
        "age",
        "children",
      ],
      "type": "object",
    },
  },
  "properties": {
    "age": {
      "type": "number",
    },
    "children": {
      "$ref": "#/definitions/Other_4",
    },
    "name": {
      "type": "string",
    },
  },
  "required": [
    "name",
    "age",
    "children",
  ],
  "type": "object",
}

1.6 Index Types

import { AAA } from './common';

interface Interface_14_2 {
  [attr: string]: {
    name: string;
    other: AAA;
  };
}

result:

{
  "additionalProperties": {
    "properties": {
      "name": {
        "type": "string",
      },
      "other": {
        "$ref": "#/definitions/AAA",
      },
    },
    "required": [
      "name",
      "other",
    ],
    "type": "object",
  },
  "definitions": {
    "AAA": {
      "additionalProperties": false,
      "properties": {
        "other1": {
          "type": "string",
        },
      },
      "required": [
        "other1",
      ],
      "type": "object",
    },
  },
  "type": "object",
}