Nicolas S.Xu Nicolas S.Xu - 1 year ago 145
TypeScript Question

ts-node will not compile official Discriminated Unions example

Here is the official example for discriminated Unions from here. It won't pass the compiling through ts-node. The error message is:

15.discriminated_unions.ts (29,33): Cannot find name 'never'. (2304)

15.discriminated_unions.ts (34,33): Property 'size' does not exist on type 'Square | Rectangle | Circle'. (2339)

But it will successfully pass the compiling in Typescript Playground.

What do I miss? Is there a compile options?

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

Answer Source

ts-node brings in the 1.8 compiler (see its package.json on github). You can ask the package author to update to the 2.0 compiler.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download