1

The key difference on this question is I want to keep my property referenced inside the object, not destructured.

export interface MapSettings {
  up: 'trueNorth' | 'runIn' | 'magneticNorth' | 'user';
  rotation?: number;
}

type MapProps = {
  settings: MapSettings;
};

export const Map: FunctionComponent<MapProps> = function Map({
  settings,
}) {

I want to set a default value for settings.rotation but I also want to keep it referenced as settings.rotation because settings has a lot more properties than shown here and I know where the value is coming from.

This is the best answer I can come up with:

export const Map: FunctionComponent<MapProps> = function Map({
  settings: { rotation: settingsRotation = 360, ...settings },
}) {

But it doesn't let me reference settings.rotation with a default value of 360.

Diesel
  • 5,099
  • 7
  • 43
  • 81

2 Answers2

1

just check it and set it:

export const Map: FunctionComponent<MapProps> = function Map({
  settings,
}) {
  settings.rotation = (settings.rotation || settings.roation === 0) ? settings.rotation : 360
bryan60
  • 28,215
  • 4
  • 48
  • 65
0

Default value is mentioned as { someKey = 'defaultValue' } insead of `{ someKey: 'defaultValue' }

So you can write it like

{
   settings = { rotation: settingsRotation = 360 },
}
Zohaib Ijaz
  • 21,926
  • 7
  • 38
  • 60