2017-06-20 90 views
1
const changeToInvalidations = (change: Change): Array<Invalidation> => { 
    switch (change.resourceType) { 
    case "brand": 
     switch (change.type) { 
     case "added": 
      return [{ type: "page", value: "productListing" }] 
     case "updated": 
      return [{ type: "brand", value: change.resourceId }] 
     case "removed": 
      return [{ type: "brand", value: change.resourceId }] 
     } 
    case "product": 
     switch (change.type) { 
     case "added": 
      return [{ type: "page", value: "productListing" }] 
     case "updated": 
      return [{ type: "product", value: change.resourceId }] 
     case "removed": 
      return [ 
      { type: "page", value: "productListing" }, // pagination reflow 
      { type: "brand", value: change.resourceId }, 
      ] 
     } 
    } 
} 

Change類型的定義:TypeScript開關盒徹底檢查不起作用?

type Change = { 
    type: "added" | "updated" | "removed" 
    resourceId: number 
    resourceType: "brand" | "product" 
} 

錯誤:

Function lacks ending return statement and return type does not include 'undefined'

但是從閱讀本https://www.typescriptlang.org/docs/handbook/advanced-types.html

看來我應該是好去。我做錯了什麼?

+1

如何定義Change? – Saravana

+0

@Saravana我用它的定義更新了描述。希望能幫助到你! –

+0

可能與[this discussion](https://github.com/Microsoft/TypeScript/issues/8618)有關。 – Saravana

回答

0

錯誤消息可能指的是缺少default:的情況,其中沒有定義返回語句。如果resourceTypetype屬性是undefined它將切換到default的情況。

+0

'resourceType'和'type'屬性是無空的,所以我不認爲是這種情況。請參閱我更新的描述,其中定義了「更改」類型。 –