这里有一段代码:
export default function (triggerEl: Ref<HTMLDivElement>, contentEl: Ref<HTMLDivElement>, placement: Ref<Placement> | Placement = 'auto', offset: Ref<number> | number = 0) {
placement = ref(placement) as Ref<Placement>
offset = ref(offset)
const { top: triggerTop, right: triggerRight, bottom: triggerBottom, left: triggerLeft, width: triggerWidth, height: triggerHeight } = useElementBounding(triggerEl)
const { top: contentTop, right: contentRight, bottom: contentBottom, left: contentLeft, width: contentWidth, height: contentHeight } = useElementBounding(contentEl)
const position = computed<PopperPosition>(() => {
let top = Number.MIN_SAFE_INTEGER
let left = Number.MIN_SAFE_INTEGER
if (placement.value === 'top') {
top = triggerTop.value - contentHeight.value - offset.value
left = triggerLeft.value + triggerWidth.value / 2 - contentWidth.value / 2
}
return {
top,
left,
}
})
return null
}
我已经将'placement'和'offset'参数转换为它们对应的'Ref'类型,但下面的代码仍然认为它们是Ref类型|number,所以它会抱怨,但它们实际上永远不会是number类型。
如何解决此处的键入检查问题???
1条答案
按热度按时间agxfikkp1#
我假设扰乱TS推理的源代码位于第21和22行(重新分配变量值):
我建议创建新的变量,所以最终的代码看起来像这样: