我有一个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