我已经有了一些接口,我想用这个接口来描述模型,就像下面的代码一样。否则,我必须使用types of mobx-state-tree再次编写。但这不是正确的办法,什么是有效的解决办法?
import { types } from 'mobx-state-tree';
export interface IPeople {
name: string;
age: number;
}
const Peoples = types
.model({
name: 'peoples',
nancy: IPeople, // error at this line
})
export default Peoples;发布于 2018-10-16 06:13:25
从TypeScript类型声明到mobx-state-tree模型定义是没有办法的(除了可能通过元数据反射,尽管我怀疑有人已经实现了)。但是,如果您编写了mobx-state-tree模型定义,那么您可以从它生成TypeScript类型;参见自述文件中的在设计时使用MST类型。因此,您必须转换现有的接口,但至少您不必继续维护相同信息的两个副本。
import { types, Instance } from 'mobx-state-tree';
const Person = types.model({
name: types.string,
age: types.number
});
export type IPeople = Instance<typeof Person>;
const Peoples = types
.model({
name: 'peoples',
nancy: Person
})
export default Peoples;https://stackoverflow.com/questions/52828488
复制相似问题