예:
interface Todos {
id: string | number;
title: string | number;
content: string | number;
isDone: boolean;
[other: string]: any;
}
other라는 key의 타입에 string, number, symbol, Template literal 타입이 아닌 any같은 다른 타입을 선언하면 에러가 난다.
<h1 id='title'>내용</h1>
let tagTitle = document.querySelector('#title')
if(tagTitle != null){ //narrowing
tagTitle.innerHTML = '바꿀 내용'
}
// narrowing 방법2
if(tagTitle instanceof Element){
tagTitle.innerHTML = '바꿀 내용'
}
// narrowing 방법3 (확실할 때 아니면 권장하지 않음)
let tagTitle = document.querySelector('#title') as Element;
// narrowing 방법4 옵셔널 체이닝
replace: true가 window.location.replace의 역할을 함
const navigate = useNavigate();
navigate('/', { replace: true }