2016-09-22 4 views
0

차별 노동 조합에 대한 공식 예는 here입니다. 컴파일은 ts-node을 통과하지 않습니다. 오류 메시지는 다음과 같습니다.ts-node는 공식적인 차별 노동 조합을 작성하지 않습니다. 예 :

15.discriminated_unions.ts (29,33) : 'never'라는 이름을 찾을 수 없습니다. (2304)

15.discriminated_unions.ts (34,33) : '크기'속성이 '유형 | 직사각형 | 원'. (2339)

그러나 Typescript 놀이터에서 컴파일을 성공적으로 통과합니다.

무엇이 놓치나요? 컴파일 옵션이 있습니까?

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 
    } 
} 

답변

2

ts-node은 (GitHub의에의 package.json 참조) 1.8 컴파일러에서 제공합니다. 패키지 작성자에게 2.0 컴파일러로 업데이트하도록 요청할 수 있습니다.

+0

'ts-node'는 사용 가능한 경우 로컬 컴파일러를 자동으로 선택합니다 – basarat

2

ts-node은 로컬 타이프 스크립트 설치를 자동으로 선택합니다. 따라서 최신 타이프 스크립트 npm install [email protected] --save-dev을 프로젝트 폴더에 설치 한 다음 프로젝트 폴더에서 ts-node를 실행하십시오.

관련 문제