【发布时间】:2020-07-22 05:03:52
【问题描述】:
这个问题的主要区别是我希望将我的属性保留在对象内部,而不是解构。
export interface MapSettings {
up: 'trueNorth' | 'runIn' | 'magneticNorth' | 'user';
rotation?: number;
}
type MapProps = {
settings: MapSettings;
};
export const Map: FunctionComponent<MapProps> = function Map({
settings,
}) {
我想为settings.rotation 设置一个默认值,但我也想将其引用为settings.rotation,因为设置的属性比此处显示的要多得多,而且我知道值的来源。
这是我能想到的最佳答案:
export const Map: FunctionComponent<MapProps> = function Map({
settings: { rotation: settingsRotation = 360, ...settings },
}) {
但它不允许我使用默认值 360 引用 settings.rotation。
【问题讨论】:
-
只需检查函数第一行中的属性,如果未设置则设置它
标签: javascript reactjs typescript