小编lam*_*utz的帖子

打字稿使用Observable.of区分联合类型

我正在尝试将Typescript 2.0的已区分联合类型与RxJS一起使用,但是却收到一个错误,即我返回的对象不是联合类型的类型之一。

这是我的类型:

interface Square {
  kind: "square";
  width: number;
}

interface Circle {
  kind: "circle";
  radius: number;
}

interface Center {
  kind: "center";
}

type Shape = Square | Circle | Center;
Run Code Online (Sandbox Code Playgroud)

我只返回一个Shape不使用Observable编译器的函数完全可以:

function shapeFactory(width: number): Shape {
  if (width > 5) {
    return {kind: "circle", radius: width};
  } else if (width < 2) {
    return {kind: "square", width: 3};
  }

  return {kind: "center"};
}
Run Code Online (Sandbox Code Playgroud)

当我改为尝试返回Observable<Shape>类似的内容时:

function shapeFactoryAsync(width: number): Observable<Shape> {
  if (width …
Run Code Online (Sandbox Code Playgroud)

discriminated-union observable rxjs typescript

5
推荐指数
1
解决办法
753
查看次数

在 Goland IDE 中使用非标准 SQL 包时的语法高亮 SQL 查询

使用 Goland 时,根据我连接的数据源获取 SQL 语法突出显示和自动完成非常有用。不幸的是,这似乎只在我使用标准 Go sql 包时有效,而在我使用有效包装 sql 包调用的自定义包时不起作用。我想知道是否可以告诉 Goland 特定的函数/参数实际上是 SQL 查询/SQL 语句。

这是 Goland 的示例,允许对 sql.DB 结构上的方法进行 SQL 补全,而不允许在自定义 query.ReadOnlyDB 结构上补全:

适用于 sql.DB 不适用于自定义查询。ReadOnlyDB

sql go goland

1
推荐指数
1
解决办法
63
查看次数

标签 统计

discriminated-union ×1

go ×1

goland ×1

observable ×1

rxjs ×1

sql ×1

typescript ×1