0.1.2 • Published 2 months ago

interface2class v0.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

interface2class

一个通过 interface 生成 class 的命令行工具,适用于 TSArkTS

安装

npm i interface2class -g

使用

  • 文件 demo.ts
enum Gender {
  MAN = 'men',
  WOMAN = 'women'
}

interface Response {
  code: number
  message: string
  data: User
}

interface User {
  nickname: string
  age: number
  avatar: ResourceStr
  createAt: Date 
  gender: Gender
  hobby: string[]
  follows: User[]
  isValid: 0 | 1
}
  • 执行命令
i2c ./demo.ts
  • 自动写入 demo.ts
enum Gender {
  MAN = 'men',
  WOMAN = 'women'
}

interface Response {
  code: number
  message: string
  data: User
}

interface User {
  nickname: string
  age: number
  avatar: ResourceStr
  createAt: Date 
  gender: Gender
  hobby: string[]
  follows: User[]
  isValid: 0 | 1
}

// auto gen →

export class ResponseModel implements Response {
  code: number = 0
  message: string = ''
  data: User = new UserModel({} as User)

  constructor(model: Response) {
    this.code = model.code
    this.message = model.message
    this.data = model.data
  }
}
export class UserModel implements User {
  nickname: string = ''
  age: number = 0
  avatar: ResourceStr = ''
  createAt: Date = new Date()
  gender: Gender = Gender.MAN
  hobby: string[] = []
  follows: User[] = []
  isValid: 0 | 1 = 0

  constructor(model: User) {
    this.nickname = model.nickname
    this.age = model.age
    this.avatar = model.avatar
    this.createAt = model.createAt
    this.gender = model.gender
    this.hobby = model.hobby
    this.follows = model.follows
    this.isValid = model.isValid
  }
}

格式化 Interface

如果出现生成 class 失败,请格式化后再生成

  • 文件 demo.ts
/**
* 报文数据
*/
export interface Data {
  /**
   * 总页数
   */
  pageTotal?: number;
  /**
   * 数据集合
   */
  rows?: Row[];
  /**
   * 总数
   */
  total?: number;
  [property: string]: any;
}
  • 执行命令
i2c format ./demo.ts
  • 格式化 demo.ts
/** 报文数据 */
export interface Data {
  /** 总页数 */
  pageTotal: number | null;
  /** 数据集合 */
  rows: Row[] | null;
  /** 总数 */
  total: number | null;
}
0.1.2

2 months ago

0.1.1

4 months ago

0.0.13

5 months ago

0.0.14

5 months ago

0.1.0

5 months ago

0.0.10

5 months ago

0.0.11

5 months ago

0.0.12

5 months ago

0.0.9

5 months ago

0.0.8

5 months ago

0.0.7

5 months ago

0.0.6

5 months ago

0.0.5

5 months ago

0.0.4

5 months ago

0.0.3

5 months ago

0.0.2

5 months ago

0.0.1

5 months ago