2

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

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

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

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

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

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

4

1 回答 1

1

尝试使用Partial实用程序类型- 可能正是您正在寻找的:

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

export type InputStyle = Partial<Style>;

于 2020-05-18T20:33:18.690 回答