Как можно упростить/оптимизировать данный код?

В частности похожие части кода с присвоением значений переменным

private isThumbsCollision(): boolean {
  let fromEdge: number;
  let toEdge: number;
  let thumbSize: number;

  if (this.settings.isVertical === false) {
    thumbSize = this.to.tooltip.element.getBoundingClientRect().width;
    fromEdge = this.from.element.getBoundingClientRect().right;
    toEdge = this.to.element.getBoundingClientRect().right;
  } else {
    thumbSize = this.to.tooltip.element.getBoundingClientRect().height;
    fromEdge = this.from.element.getBoundingClientRect().top;
    toEdge = this.to.element.getBoundingClientRect().top;
  }
  return toEdge - fromEdge <= thumbSize;
}


Ответы (2 шт):

Автор решения: Эникейщик

Ну вот так, например, проще и оптимизированней

if (this.settings.isVertical) {
    thumbSize = this.to.tooltip.element.getBoundingClientRect().height;
    fromEdge = this.from.element.getBoundingClientRect().top;
    toEdge = this.to.element.getBoundingClientRect().top;
  } else {
    thumbSize = this.to.tooltip.element.getBoundingClientRect().width;
    fromEdge = this.from.element.getBoundingClientRect().right;
    toEdge = this.to.element.getBoundingClientRect().right;
  }
→ Ссылка
Автор решения: Не быть рабом на Руси

Читаемый вариант:

const size_property = this.settings.isVertical
 ? 'height'
 : 'width';

const edge_property = this.settings.isVertical
 ? 'top'
 : 'right';

let thumbSize = this.to.tooltip.element.getBoundingClientRect()[size_property];
let fromEdge = this.from.element.getBoundingClientRect()[edge_method];
let toEdge = this.to.element.getBoundingClientRect()[edge_property];

Вырви глаз (зато в три строки):

let thumbSize = this.to.tooltip.element.getBoundingClientRect()[this.settings.isVertical ? 'height': 'width'];
let fromEdge = this.from.element.getBoundingClientRect()[this.settings.isVertical ? 'top' : 'right'];
let toEdge = this.to.element.getBoundingClientRect()[this.settings.isVertical ? 'top' : 'right'];
→ Ссылка