Type Guard
νμ κ°λλ νμ μ€ν¬λ¦½νΈμμ λ³μμ νμ μ λ³΄λ€ κ΅¬μ²΄μ μΌλ‘ μ’ν μ½λ μμ μ±μ λμ΄κΈ° μν΄ μ¬μ©λλ ν€μλμ΄λ€.
μ°λ¦¬κ° νμ μ€ν¬λ¦½νΈλ₯Ό μ¬μ©νλ μ΄μ λ λ³μμ νμ μ μ격νκ² νμΈνμ¬ λ°μ κ°λ₯μ±μ΄ μλ μ¬λ¬ μλ¬ λ€μ λ°νμμ΄ μλ μ»΄νμΌ νμμ μ‘μλ΄κΈ° μν΄μ μ΄λ€. μ½κ² λ§ν΄ νμ μ€ν¬λ¦½νΈκ° λ³μμ νμ μΆλ‘ μ λ μννκ² νκΈ° μν΄ μ¬μ©λλ κ²μ΄λ€.
κ°λ¨ν μμλ₯Ό λ€μ΄λ³΄μ.
fuction SampleFn (data: number | string) : void {
console.log(data + 1);
}
μμ μμ μ½λλ₯Ό μ€ννλ©΄ ν¨μμ λ§€κ°λ³μκ° number νμ μΈμ§ stringνμ μΈμ§ νμ€νμ§ μμλ° λ§€κ°λ³μ + 1μ μ€ννλ €κ³ νκΈ° λλ¬Έμ μλ¬κ° λ°μνλ€. μ΄λ° κ²½μ°, λ§€κ°λ³μμ νμ μ λ°λΌ μ²λ¦¬λ₯Ό λΆκΈ° ν μ μλλ‘ νμ κ°λλ₯Ό μ¬μ©νλ€.
μ£Όλ‘ typeof, instanceof, in
3 κ°μ§ μ°μ°λ€μ΄ μ¬μ©λλ€.
typeof
λ¨Όμ typeof μ°μ°μλ μλ°μ€ν¬λ¦½νΈμμλ μ¬μ©μ΄ κ°λ₯νλ©°, μ΄λ¦ κ·Έλλ‘ λ³μμ νμ μ λ°ννλ€.
const v1 = typeof "first";
const v2 = typeof 2;
const v3 = typeof true;
const v4 = typeof { age : 10};
console.log(v1, v2, v3, v4) // string number boolean object
λλ¬Έμ νμΈνκ³ μ νλ λ³μμ νμ μ μ§κ΄μ μ΄κ³ κ°λ¨νκ² νμ νμ¬ μ½λλ₯Ό μμ±ν μ μλ€.
typeof λ₯Ό μ¬μ©ν΄ μμ μμ μ½λλ₯Ό λ°κΏλ³΄λ©΄
function SampleFn (data: number | string){
if (typeof data === "string") console.log(`${data} + 1`)
else console.log(data + 1)
}
μ΄λ κ² κ° νμ λ³λ‘ λΆκΈ° μ²λ¦¬λ₯Ό ν΄ λ€λ₯Έ μΆλ ₯ κ²°κ³Όκ° λμ€κ²λ ν μ μλ€.
typeof μ°μ°μλ‘ λ°νλλ νμ λ€μ μλμ κ°λ€.
string, number, bigint, boolean, symbol, undefined, object, function
in
in μ°μ°μλ typeofμ λ§μ°¬κ°μ§λ‘ μλ°μ€ν¬λ¦½νΈμ μ‘΄μ¬νλ€. λ³μκ° νΉμ μμ±μ κ°μ§κ³ μλμ§ νμΈνμ¬ boolean κ°μ λ°ννλ€.
interface User {
name: string;
age: number;
address?: string;
}
const user1: User = {
name: 'aug',
age: 20,
};
const user2: User = {
name: 'kuku',
age: 25,
address: 'μμΈμ',
};
function hasAddress(user: User): boolean {
return 'address' in user;
}
console.log(hasAddress(user1)); // false
console.log(hasAddress(user2)); // true
User μΈν°νμ΄μ€μ μ΅μ
λ νλ‘νΌν°μΈ address
λ₯Ό μμ±νμ¬ ν΄λΉ νλ‘νΌν°λ₯Ό κ°μ§λ κ°μ²΄, κ°μ§μ§ μλ κ°μ²΄λ₯Ό μμ±νλ€. in μ°μ°μλ₯Ό ν΅ν΄ ν΄λΉ νλ‘νΌν°λ₯Ό κ°μ§κ³ μλμ§ νμΈνλ μ½λμ΄λ€.
instanceof
instanceof μ°μ°μλ νμΈνλ λ³μκ° μ΄λ€ ν΄λμ€μΈμ§, μ΄λ€ ν΄λμ€λ₯Ό μμ λ°μλμ§ νμΈνλ μ°μ°μμ΄λ€. λ§μ΄ μ΄μ§ μ΄λ €μ°λ―λ‘ κ°λ¨ν μμ μ½λλ₯Ό ν΅ν΄ μμ보μ.
class User {
name: string;
constructor(name: string) {
this.name = name;
}
introduction(): void {
console.log(`Hi my name is ${this.name}`);
}
}
class AdminUser extends User {
constructor(name: string) {
super(name);
}
introduction(): void {
console.log(`Hi my name is ${this.name}, I am an administrator user.`);
}
}
class GuestUser extends User {
constructor(name: string) {
super(name);
}
introduction(): void {
console.log(`Hi my name is ${this.name}, I am an Guest User`);
}
}
const user1 = new AdminUser("aug");
const user2 = new GuestUser("kuku");
console.log(user1 instanceof User); // true
console.log(user1 instanceof AdminUser); // true
console.log(user1 instanceof GuestUser); // false
console.log(user2 instanceof User); // true
console.log(user2 instanceof AdminUser); // false
console.log(user2 instanceof GuestUser); // true
μ μ½λμμ AdminUser ν΄λμ€μ GuestUser ν΄λμ€λ User ν΄λμ€λ₯Ό μμλ°λλ€. κ° ν΄λμ€μ μΈμ€ν΄μ€μΈ user1κ³Ό user2λ₯Ό instanceof μ°μ°μλ‘ νμΈν΄λ³΄λ©΄ λΏλ¦¬ ν΄λμ€μΈ Userμ κ° μΈμ€ν΄μ€μ λΆλͺ¨ ν΄λμ€μ trueλ₯Ό λ°ννμ§λ§ λ€λ₯Έ ν΄λμ€λ falseκ° λμ€λ κ²μ νμΈ ν μ μλ€.
νμλ μ£Όλ‘ try catch λ¬Έμ ν΅ν΄ μλ¬λ₯Ό νΈλ€λ§ ν λ instanceof μ°μ°μλ₯Ό μ¬μ©νλ€.
import axios, { AxiosError } from 'axios';
class ApiError extends Error {
constructor(message: string) {
super(message);
this.name = 'ApiError';
}
}
async function fetchData() {
try {
const response = await axios.get(API_ENDPOINT);
console.log('Data:', response.data);
} catch (error) {
if (error instanceof AxiosError) {
// AxiosError νμ
μ μλ¬μΈ κ²½μ°
console.error(`AxiosError: ${error.message}`);
throw new ApiError('Failed to fetch data from the API.');
} else {
// λ€λ₯Έ μ’
λ₯μ μλ¬μΈ κ²½μ°
console.error(`Unknown error: ${error}`);
throw new ApiError('An unknown error occurred.');
}
}
}
Last updated