我有一個react-redux應用程序,使用不可變包裝在typescript中編寫。在那裏我有一個數據,它來自api,並且在商店中我將它包裝到Map中。在所有應用程序中,它們都被用作Map。TypeScript |不可變|適當的方式來擴展Immutable.Map類型
我創建了一個接口:
export interface PaymentMethod extends Immutable.Map<string, string | NamedType<number>> {
id: string;
name: string;
description: string;
accountNr: string;
paymentMethodType: NamedType<number>;
}
一般來說它的作品非常好。除了測試,在那裏我創建的數據是這樣的:
const dummyPaymentMethod: PaymentMethod = Map({
id: '',
name: '',
description: '',
accountNr: '',
paymentMethodType: { id: 1, name: '' },
});
,然後我得到一個皮棉錯誤:
Error:(116, 13) TS2322:Type 'Map<string, string | { id: number; name: string; }>' is not assignable to type 'PaymentMethod'.
Property 'id' is missing in type 'Map<string, string | { id: number; name: string; }>'.
我覺得完全失去了,因爲我可以在界面和我的虛擬數據看ID。
我會欣賞一些光。我覺得不知何故,我應該通過可接受的鍵列表到我的地圖,但不知道,如何做到這一點。
編輯:拼錯
謝謝,我從你的答案中得到了非常有用的信息。 – Kania
這是否適用於'Record'(只帶'get')? – ton