2017-06-20 86 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'属性是无空的,所以我不认为是这种情况。请参阅我更新的描述,其中定义了“更改”类型。 –