-
Notifications
You must be signed in to change notification settings - Fork 7
π 10μ 27μΌ μ€ν¬λΌ κΈ°λ‘
Chun Hwa Park edited this page Oct 27, 2021
·
2 revisions
νμ μ€ν¬λ¦½νΈ νΈλλΆμΌλ‘ κ°μ 곡λΆν λ€(ν¨μ, 리ν°λ΄ νμ , μ λμΈκ³Ό κ΅μ°¨ νμ ), λλ μ μ 곡μ
- ν¨μ, 리ν°λ΄ νμ , μ λμΈκ³Ό κ΅μ°¨νμ μ 곡λΆν λ§ νλ€.
- μΈν°νμ΄μ€κ° λ μ΄λ €μ λ€.
TS Playground - An online editor for exploring TypeScript and JavaScript
interface Fish {
swim(): void;
layEggs(): void;
}
interface Bird {
fly(): void;
layEggs(): void;
}
const fish: Fish = {
swim() {
console.log('swim');
},
layEggs() {
console.log('layEggs');
}
};
const bird: Bird = {
fly() {
console.log('fly');
},
layEggs() {
console.log('layEggs');
}
};
function getPet(): Fish | Bird {
return [fish, bird][Math.random()*2];
}
const pet = getPet();
pet.layEggs(); // κ³΅ν΅ μμ±μ΄κΈ° λλ¬Έμ λ°λ‘ μ¬μ©κ°λ₯
(pet as Bird).fly(); // μΊμ€ν
μ ν΄μΌ μ¬μ©κ°λ₯
(pet as Fish).swim(); // μ΄λμμ μ€λ₯κ° λ°μν μ§ λͺ¨λ₯Έλ€ -> BAD
- Interfaceλ μ μΈ λ³ν©μ΄ κ°λ₯νμ§λ§ Typeμ κ·Έλ μ§ μλ€.(μ μΈ λ³ν©μ΄λ
interface Window {
title: string;
}
interface Window {
ts: import("typescript");
}
μ΄λ° μμΌλ‘ μ μΈνλ©΄ μ»΄νμΌ μμ μ ν©μ³μ Έ Windowκ° titleκ³Ό tsμμ±μ λͺ¨λ κ°μ§κ² λλ€.)
κ²½κΈ°μ¬μ²μ wiki μ λλ€. π