为嵌套的变形道具添加类型打字稿React Redux TS2339

问题描述

{ "results_": [ { "alternatives_": [ { "transcript_": "but what if somebody decides to break it be careful that you keep adequate coverage but look for places to save money baby it\u0027s taking longer to get things squared away then the bankers expected hiring the life for one\u0027s company may win her tax aided retirement income the booth just helpful but inadequate new self to saving Rags or hardly tossed on the two naked bones what it discussion Canyon Sue when the title of this type of song is in question there\u0027s no dying or waxing or gassing need a paperweight maybe personalized kNown back Quoc Leigh is leather hard place work on a flat surface and smooth out this simplest kind of separate system uses a single self-contained unit to the old shop outage still Holts a good mechanic is usually a bad boss so fingers would go higher in later years so make beautiful chairs cabinets chest doll houses it\u0027s at","confidence_": 0.6881865,"words_": [ { "startTime_": { "seconds_": 0,"nanos_": 0,"memoizedisInitialized": -1,"unkNownFields": { "fields": {},"fieldsDescending": {} },"memoizedSize": -1,"memoizedHashCode": 0 },"endTime_": { "seconds_": 0,"nanos_": 200000000,"word_": "but","confidence_": 0.53119344,"speakerTag_": 0,"unkNownFields": { "fields": {},"fieldsDescending": {} },"memoizedHashCode": 0 },...... 函数的变形道具(来自redux状态)是嵌套的,如何将mapStatetoProps()类型应用于它?编译后收到以下错误消息:

HeaderStateMap

此外,我同时设置了Property 'cart' does not exist on type 'HeaderStateMap'. TS2339 ,但是如果我将类型更改为object,TypeScript会抱怨user:any

不胜感激:)。

代码

Property 'user' does not exist on type '{}'

解决方法

您可以嵌套您的类型:

interface User {
  name: string;
};

type HeaderStateMap = {
  user: User;
  cart: {
    hidden: boolean;
  };
};

关于您所遇到的错误,我认为您的结构破坏得太深了,假设您在用户对象user.user中拥有用户,请尝试:

const mapStateToProps = ({ user,cart: { hidden } }:HeaderStateMap) =>

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...