2017-09-27 32 views
0

我描述这些接口:无法转换配合有效的打字稿值strore

enum ProductType {current, closed, coming} 

export interface CurrentProductForCarousel { 
type_product:ProductType.current; 
offers: ProductMainInfo[] 
} 

export interface ProductMainInfo { 
    id: number; 
    disclaimer: string; 
    company?: { 
    registeredOfficeState?: string; 
    }; 
    date: { 
    timestamp: number; 
    days: number; 
    hours: number; 
    }; 
} 

我有NGRX店。我减速的样子响应

export interface State { 
currentProductForCarousel: CurrentProductForCarousel | null; 
} 

export const initialState: State = { 
сurrentProductForCarousel: null, 
}; 

export function reducer(state = initialState, action: 
pruduct.Actions): State { 
switch (action.type) { 
    case pruductActions.FETCH_PRODUCTS_CURRENT_SUCCESS: { 
    return { 
     ...state, 
    currentProductsForCarousel: action.payload, 
    }; 
    } 

{"success":true, "type_prudct":"current","products":[{"id":34, "disclaimer": "text", "company":{"registeredOfficeSuburb":"West Perth"}, "date":{"timestamp":1567987198,"days":710,"hours":"14"}}]} 

问题是我怎么可以用正确类型的界面我来存储和比从商店获得的数据集的响应数据?

回答

0

响应中的类型与枚举不匹配,因为它是一个字符串。您可以如下表示:

type ProductType = 'current' | 'closed' | 'coming'; 

interface Company { 
    registeredOfficeSuburb: string; 
} 

interface ProductDate { 
    timestamp: number; 
    days: number; 
    hours: string; 
} 

interface Product { 
    id: number; 
    disclaimer: string; 
    company: Company; 
    date: ProductDate; 
} 

interface ProductResponse { 
    success: boolean; 
    type_prudct: ProductType, 
    products:Product[] 
} 

var r: ProductResponse = { 
    "success": true, 
    "type_prudct": "current", 
    "products": [ 
     { 
      "id": 34, 
      "disclaimer": "text", 
      "company": { "registeredOfficeSuburb": "West Perth" }, 
      "date": { "timestamp": 1567987198, "days": 710, "hours": "14" } 
     }] 
};