2017-09-27 3 views
0

konvertiert ich diese Schnittstellen beschrieben:Kann nicht Antwort auf gültigen Typoskript Wert für 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; 
    }; 
} 

Ich habe NGRX-Speicher. Mein Minderer sieht aus wie

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, 
    }; 
    } 

Beispiel für die Antwort

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

Frage ist, wie ich Antwortdaten mit der richtigen Art von meiner Schnittstelle und als Daten aus Speicher erhalten speichern einstellen kann?

Antwort

0

Der Typ in Ihrer Antwort stimmt nicht mit der Enumeration überein, da es sich um eine Zeichenfolge handelt. Sie können es jedoch wie folgt darstellen:

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" } 
     }] 
}; 
Verwandte Themen