TypeSwift

一套将 TypeScript 模型解析为 Swift 模型的工具集

特性

如何使用

安装

.package(url: "https://github.com/valdirunars/TypeSwift.git", from: "0.0.14")

用法

try! TypeSwift.sharedInstance.convert(file: fileURL,
                                 to: .swift,
                                 output: outURL)

// or alternatively
let string: String? = TypeSwift.sharedInstance.convertedString(from: typescript, to: .swift)

示例转换

以下 TypeScript 字符串

interface Protocol {
    readonly y: number
}

export class Foo implements Protocol {
    public x: number = 3;
    private readonly y: number;

    constructor(x: number, y: number) {
      this.x = x
      this.y = y
    }
}

class Bar {
    protected property : Array<[boolean, string]>

    constructor(property: [(Bool, String)]) {
      this.property = property
    }
}

将会被转换为

protocol Protocol {
    var y: Double { get }
}

public struct Foo: Protocol {
    public var x: Double = 3
    private let y: Double

    init(_ x: Double, _ y: Double) {
      self.x = x
      self.y = y
    }
    init(x: Double, y: Double) {
      self.x = x
      self.y = y
    }
}

struct Bar {
    internal var property: [(Bool, String)]

    init(_ property: [(Bool, String)]) {
      self.property = property
    }

    init(property: [(Bool, String)]) {
      self.property = property
    }
}