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
看來我應該是好去。我做錯了什麼?
如何定義Change? – Saravana
@Saravana我用它的定義更新了描述。希望能幫助到你! –
可能與[this discussion](https://github.com/Microsoft/TypeScript/issues/8618)有關。 – Saravana