类型'typeof <classname>'的参数不能分配给'new()=> <classname>'类型的参数

时间:2019-06-07 11:36:08

标签: typescript

我收到以下错误

    public List<String> completeArea(String inputName) {
        try {
            Class.forName("com.mysql.jdbc.Driver");

            String query1 = "select name from labels where name like ? order by name";
            try (Connection con = DriverManager.getConnection(
                            "jdbc:mysql://localhost/company", "root", "");
                    PreparedStatement pst = con.prepareStatement(query1)) {

                pst.setString(1, inputName + "%");
                try (ResultSet rs = pst.executeQuery()) {}

                    List<String> result = new ArrayList<>();
                    while (rs.next()) {
                        result.add(rs.getString(1));
                    }
                    return result;
                }
            }
        } catch (Exception ex) {
            throw new IllegalStateException(ex);
        }
    }

在下一行

Argument of type 'typeof Settings' is not assignable to parameter of type 'new () => Settings'

json2typescript的文档说我应该将new JsonConvert().deserialize(Settings.Get(), Settings) 作为第二个参数。他们的例子就像我的。

这里是classReference类的简化

Settings

1 个答案:

答案 0 :(得分:2)

deserializeObject需要一个带有不带参数的构造函数的类。

export default class Settings {
  constructor(a?: number) {
    this._a = a!;
  }

  @JsonProperty('_a', Number)
  private _a: number;

  get A() {
    return this._a;
  }

  set A(newA: number) {
    this._a= newA;
  }

  static Get(): string {
    return "{}";
  }


  static GetObj(): Settings {
    return new JsonConvert().deserializeObject(Settings.Get(), Settings);
  }
}