【问题标题】:Mobx with TypeScript incompatible types error带有 TypeScript 不兼容类型错误的 Mobx
【发布时间】:2021-10-06 15:26:31
【问题描述】:

我正在尝试将车辆数组设置为我从商店获得的数组。但是,Typescript 似乎在抱怨什么。

我的模型如下所示:

import { types } from 'mobx-state-tree';
export const Vehicle = types.model('Vehicle', {
  model: types.string,
  vrm: types.string,
});

export const VehicleDetails = types.model('Vehicle Details', {
  policyNumber: types.string,
  vehicles: types.array(Vehicle),
});

我正在尝试使用这里的商店:

export const VehicleDetails: React.FC = observer(() => {
  const { dashboardStore } = useStores();
  const [policyNumber, setPolicyNumber] = useState<string | null>('');
  const [vehicles, setVehicles] = useState<Instance<typeof Vehicle[]> | null>(null);

  useEffect(() => {
    if (!dashboardStore.dashboard) {
      if (!dashboardStore.isLoading) {
        dashboardStore.fetchPolicyDetails('P0000700587');
      }
    }
  }, []);
  useEffect(() => {
    if (dashboardStore.vehicleDetails) {
      const { policyNumber, vehicles } = dashboardStore.vehicleDetails;
      setPolicyNumber(policyNumber);
      setVehicles(clone(vehicles) as Instance<typeof Vehicle[]>);
    }
  }, [dashboardStore.vehicleDetails]);

但是,我似乎无法克服这个错误

 setVehicles(clone(vehicles) as Instance<typeof Vehicle[]>);

给我一​​个错误。

我收到的错误如下:

Conversion of type 'IMSTArray<IModelType<{ model: ISimpleType<string>; vrm: ISimpleType<string>; }, {}, _NotCustomized, _NotCustomized>> & IStateTreeNode<...>' to type 'IModelType<{ model: ISimpleType<string>; vrm: ISimpleType<string>; }, {}, _NotCustomized, _NotCustomized>[]' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
  Types of property 'push' are incompatible.
    Type '{ (...items: ({ model: string; vrm: string; } & NonEmptyObject & IStateTreeNode<IModelType<{ model: ISimpleType<string>; vrm: ISimpleType<string>; }, {}, _NotCustomized, _NotCustomized>>)[]): number; (...items: ExtractCSTWithSTN<...>[]): number; }' is not comparable to type '(...items: IModelType<{ model: ISimpleType<string>; vrm: ISimpleType<string>; }, {}, _NotCustomized, _NotCustomized>[]) => number'.
      Types of parameters 'items' and 'items' are incompatible.
        Type 'IModelType<{ model: ISimpleType<string>; vrm: ISimpleType<string>; }, {}, _NotCustomized, _NotCustomized>' is not comparable to type '{ model: string; vrm: string; } & NonEmptyObject & IStateTreeNode<IModelType<{ model: ISimpleType<string>; vrm: ISimpleType<string>; }, {}, _NotCustomized, _NotCustomized>>'.

【问题讨论】:

  • 请在所有导入的代码和框中提供可重现的示例

标签: typescript mobx mobx-react mobx-state-tree


【解决方案1】:

您的useState 类型似乎不正确。应该是:

type VehicleType = Instance<typeof Vehicle>;

// ...

  const [vehicles, setVehicles] = useState<VehicleType[] | null>(null);

【讨论】:

  • 如果您的问题得到解决,请将答案标记为已接受,谢谢。
  • 如果我使用观察者,是否需要使用setState?每当我的商店发生变化时,观察者会触发重新渲染吗?我知道我的问题是假的,但这是我第一次使用 Typescrip、MobX 和所有这些......:(
  • 是的,如果您的商店发生变化,观察者组件将重新呈现。
猜你喜欢
  • 2020-12-16
  • 1970-01-01
  • 1970-01-01
  • 2020-06-30
  • 2013-09-23
  • 2011-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多