为什么这样做:
type Foo = { x: Foo }
Run Code Online (Sandbox Code Playgroud)
但这不是:
type Bar<A> = { x: A }
type Foo = Bar<Foo>
// ^^^ Type alias 'Foo' circularly references itself
Run Code Online (Sandbox Code Playgroud)
它们不应该是等价的吗?
我有一个使用immutable包在typescript中编写的react-redux应用程序.我有一个来自api和商店的数据我把它打包到Map.在所有应用程序中,它们都用作Map.
我创建了一个界面:
export interface PaymentMethod extends Immutable.Map<string, string | NamedType<number>> {
id: string;
name: string;
description: string;
accountNr: string;
paymentMethodType: NamedType<number>;
}
Run Code Online (Sandbox Code Playgroud)
一般来说它非常好用.除了测试,我以这种方式创建数据:
const dummyPaymentMethod: PaymentMethod = Map({
id: '',
name: '',
description: '',
accountNr: '',
paymentMethodType: { id: 1, name: '' },
});
Run Code Online (Sandbox Code Playgroud)
然后我得到一个lint错误:
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; }>'.
Run Code Online (Sandbox Code Playgroud)
我觉得完全迷失了,因为我可以在界面和虚拟数据中看到id.
我会很感激它的一些亮点.我觉得我应该把可接受的键列表传递给我的Map,但不知道怎么做. …