Есть такой отрывок из vue приложения
setup() {
const date = ref<Date>(new Date())
const interval = ref<number | null>(null)
onMounted(() => {
interval.value = window.setInterval(() => {
date.value = new Date()
}, 1000)
})
onUnmounted(() => {
clearInterval(interval)
})
}
Ошибка появляется в аргументе функции clearInterval(interval), и в phpstorm звучит так:
TS2769: No overload matches this call. Overload 1 of 2, '(intervalId: Timeout): void', gave the following error. Argument of type 'Ref' is not assignable to parameter of type 'Timeout'. Type 'Ref' is missing the following properties from type 'Timeout': hasRef, refresh, [Symbol.toPrimitive], ref, unref Overload 2 of 2, '(handle?: number | undefined): void', gave the following error. Argument of type 'Ref' is not assignable to parameter of type 'number'.
Проблему решило явное указание типа аргумента interval:
setup() {
const date = ref<Date>(new Date())
const interval = ref<number | null>(null)
onMounted(() => {
interval.value = window.setInterval(() => {
date.value = new Date()
}, 1000)
})
onUnmounted(() => {
clearInterval(Number(interval))
})
}
Изучаю ts совсем недавно и не сталкивался с таким ранее. Вопрос в том насколько приемлемо такое решение и есть ли какие-то альтернативные способы?