【问题标题】:Typescript reducer initial state and option argument for react component反应组件的打字稿减速器初始状态和选项参数
【发布时间】:2023-03-24 12:38:01
【问题描述】:

我的减速器有一个类型,例如:

export type Style = {
  color: string;
  (rest...)
}

export const initialState: Style = {
  color: 'blue';
  (rest...)
}

我有一个接收样式对象的组件,属性是可选的,只是覆盖样式的当前状态。类型如下所示:

export type InputStyle = {
  color?: string;
  (rest?...)
}

所以我基本上必须创建两种重复的类型,除了一种具有所有可选属性,一种没有。有没有更好的模式呢?我的直觉告诉我这不是正确的方法。

【问题讨论】:

    标签: javascript reactjs typescript use-reducer


    【解决方案1】:

    尝试使用Partial utility type - 可能正是您想要的:

    export type Style = {
      color: string;
      (rest...)
    }
    
    export type InputStyle = Partial<Style>;
    
    

    【讨论】:

      猜你喜欢
      • 2019-01-17
      • 1970-01-01
      • 2019-05-11
      • 1970-01-01
      • 2021-03-20
      • 1970-01-01
      • 1970-01-01
      • 2021-04-05
      • 1970-01-01
      相关资源
      最近更新 更多