2016-09-30 1 views
0

나는이 책 https://hamednourhani.gitbooks.io/typescript-book/타이프 라이터의 제어 흐름 분석

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; 

//Type of function: function area1(s: Square | Rectangle | Circle): number | undefined 
function area1(s: Shape) { 
    if (s.kind === "square") { 
     return s.size * s.size; 
    } 
    else if (s.kind === "rectangle") { 
     return s.width * s.height; 
    } 
    else if (s.kind === "circle") { 
     return Math.PI * s.radius * s.radius; 
    } 
    else { 
     //Type 'Square | Rectangle' is not assignable to type 'never'. 
     const _exhaustiveCheck: never = s; 
    } 
} 

//Function lacks ending return statement and return type does not include 'undefined'. 
function area2(s: Shape): number { 
    switch (s.kind) { 
     case "square": return s.size * s.size; 
     case "rectangle": return s.width * s.height; 
     case "circle": return Math.PI * s.radius * s.radius; 
     default: const _exhaustiveCheck: never = s; //Type of s is 'never' 
    } 
} 

질문 (--strictNullChecks)에서 다음 코드 :이 개 기능 s에서

  • 이유는 무엇입니까 in expression _exhaustiveCheck: never = s 다른 유형이 있습니까? 두 경우 모두 snever 일 것으로 예상했습니다.
  • area2 함수가 반환 유형을 갖고 싶습니까 number | undefined? 그것은 나에게 정의되지 않은 것처럼 여기에서 일어날 수없는 것처럼 보인다. 내가 잘못?

답변

1

나는 책 https://hamednourhani.gitbooks.io/typescript-book/

그 책 URL이 잘못에서 다음과 같은 코드가 있습니다. 원래 하나는 http://basarat.gitbooks.io/typescript/입니다. 무료 및 오픈 소스는 사람들이 자유롭게 복제본을 만들 수 있음을 의미합니다. 별로 신경 쓰지 않고 (의도하지 않은 것처럼 보이고 나는 악의적이지 않다고 가정하고 있음), 최신 버전 링크를 언급 할 것이라고 생각했습니다. 이제 귀하의 질문에

왜 2 가지 기능이 표현식 _exhaustiveCheck에서 : never = s가 다른 유형입니까? 나는 두 경우 모두에있을 수 없다고 기대했다.

당신이 게시 한 코드는 당신이 그것을 :)

그게 전부의 벌금을 수정 가정 책의 관련 섹션 https://basarat.gitbooks.io/typescript/content/docs/types/discriminated-unions.html의 코드가 아닙니다. 두 버전 모두에 대해 never을 얻을 수 있지만 샘플을 실행할 때. 그리고 당신이 볼 수 있듯이 s은 두 경우 모두 never 유형이며 오류가 없습니다. 이 의견은 잘못된 것입니다 :

enter image description here AREA2 함수가 반환 형식 번호를 가지고 싶어하는 이유

| 정의되지 않았습니까? 그것은 나에게 정의되지 않은 것처럼 여기에서 일어날 수없는 것처럼 보인다. 내가 잘못?

그냥 모두 기능 유형 number | undefined을 갖고 싶어 명확합니다. 이는 TypeScript가 일부 영역에서는 실행 중이지만 반환되지 않는 코드가 있다는 것을 알아 냈기 때문입니다. 이 지역이 never 지역임을 알지 못했습니다. 그러나 쉽게 도울 수 있지만 단순히 never을 반환하십시오. 고정 코드 :

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; 

// Type of function: function area1(s: Square | Rectangle | Circle): number 
function area1(s: Shape) { 
    if (s.kind === "square") { 
     return s.size * s.size; 
    } 
    else if (s.kind === "rectangle") { 
     return s.width * s.height; 
    } 
    else if (s.kind === "circle") { 
     return Math.PI * s.radius * s.radius; 
    } 
    else { 
     // Type of s is never 
     const _exhaustiveCheck: never = s; 
     return _exhaustiveCheck; 
    } 
} 

// No Error 
function area2(s: Shape): number { 
    switch (s.kind) { 
     case "square": return s.size * s.size; 
     case "rectangle": return s.width * s.height; 
     case "circle": return Math.PI * s.radius * s.radius; 
     default: 
      const _exhaustiveCheck: never = s; // Type of s is 'never' 
      return _exhaustiveCheck; 
    } 
} 

PS : 나는뿐만 아니라 strictNullChecks와 함께 책을 업데이트했습니다

+0

https://github.com/basarat/typescript-book/blob/master/docs/types/discriminated-unions.md#strictnullchecks이 친절하고 성가신 질문을 알고 보니이 답변을 주셔서 감사합니다. 나는 아마 뭔가를 엉망으로 만들었고이 문제는's' 인'Square | 직사각형 '을 선택합니다. 죄송합니다. 다음 번에는 문제가 생기고 잠시 휴식을 취하고 질문을 게시 할 것입니다. 나는 또한 책의 당신의 버전을 읽을 것이고, 그것이 잘못된 버전인지 알지 못했다 :-) –