【问题标题】:ts-node will not compile official Discriminated Unions examplets-node 不会编译官方的 Discriminate Unions 示例
【发布时间】:2016-09-22 18:56:17
【问题描述】:

这是来自here 的受歧视联合的官方示例。它不会通过ts-node 进行编译。错误信息是:

15.discriminated_unions.ts (29,33):找不到名称“从不”。 (2304)

15.discriminated_unions.ts (34,33):“Square |”类型上不存在属性“size”矩形 |圆圈'。 (2339)

但是在Typescript Playground中会顺利通过编译。

我错过了什么?有编译选项吗?

interface Square {
    kind: "square";
    size: number;
}
interface Rectangle {
    kind: "rectangle";
    width: number;
    height: number;
}
interface Circle {
    kind: "circle";
    radius: number;
}


type Shape = Square | Rectangle | Circle;

function assertNever(x: never): never {
    throw new Error("Unexpected object: " + x);
}
function area(s: Shape) {
    switch (s.kind) {
        case "square": return s.size * s.size;
        case "rectangle": return s.height * s.width;
        case "circle": return Math.PI * s.radius ** 2;
        default: return assertNever(s); // error here if there are missing cases
    }
}

【问题讨论】:

    标签: typescript


    【解决方案1】:

    ts-node 引入了 1.8 编译器(参见其在 github 上的 package.json)。您可以要求包作者更新到 2.0 编译器。

    【讨论】:

    • ts-node 自动选择本地编译器(如果可用)
    【解决方案2】:

    ts-node 将自动选择您的本地打字稿安装。因此,将最新的打字稿npm install typescript@next --save-dev 安装到您的项目中,然后从您的项目文件夹中运行 ts-node ?

    【讨论】:

      猜你喜欢
      • 2017-08-05
      • 2018-03-12
      • 2021-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-14
      • 1970-01-01
      相关资源
      最近更新 更多