νμ μ’νκΈ°λ μ£Όλ‘ λͺ ννμ§ μμ νμ μ΄ μμλ μ¬μ©νλ€. κ·Έλ° νμ μ μ λμ¨ νμ μ΄λΌκ³ νλ©° μ΄λ₯Ό λ³΄λ€ λͺ ννκ² μ’νλ κ²μ΄λ€.
typeofλ string,number,booleanκ³Ό κ°μ μμ κ°μ μ²λ¦¬ν λ μ μ©νλ€.
function triple(value:number | string){
if(typeof value === "string"){
return value.repeat(3);
}
return value * 3;
}
μ°ΈμΈμ§ κ±°μ§μΈμ§ κ²μ¬ν΄μ νμ μ’νκΈ°λ₯Ό ν μ μλ€.
const printLetters = (word?: string) => {
if(word){
for(let char of word){
console.log(char);
}
}else{
console.log('not Word')
}
}
λΉκ΅ μ°μ°μλ₯Ό μ¬μ©νμ¬ νμ μ νλ³νκ³ μ’νκΈ°λ₯Ό ν μ μλ€.
function seomDemo(x: string | number, y: string | boolean){
if(x === y){
x.toUpperCase();
}
}
interface Movie{
title: string;
duration: number;
}
interface TVShow{
title: string;
numEpisodes: number;
episodeDuration: number;
}
function getRuntime(media: Movie|TVShow){
if("numEpisodes" in media){
return media.numEpisodes * media.numEpisodes;
}
return media.duration;
}
function printFullDate(date: string | Date){
if (date instanceof Date){
console.log(date.toUTCString());
}else{
console.log(new Date(date).toUTCString());
}
}
class User{
constructor(public name: string){}
}
class Company{
constructor(public name: string){}
}
function printName(entity: User | Company){
if(entity instanceof User ){
entity // User
}else{
entity // Company
}
}
typeofλ₯Ό μ¬μ©ν μ μλ κ²½μ°μ instanceofλ₯Ό μ¬μ©νκ² λλλ° κ·Έλ¬ν κ²½μ°λ new ν€μλλ₯Ό μ¬μ©ν΄ μ΄κΈ°νν ν΄λμ€λ±μΌλ‘ μμ μ νλ κ²½μ° λ±μ΄ μλ€.
parameterName is Type νμμΌλ‘ μ¬μ©λλ€.
interface Cat{
name: string;
numberLives: number;
}
interface Dog{
name: string;
breed: string;
}
// νμ
λͺ
μ λ isXX νμμ μ΄λ¦μΌλ‘ νλκ²μ΄ μΌλ°μ μ΄λ€.
function isCat(animal: Cat | Dog): animal is Cat{ // μ΄ ν¨μκ° trueλ₯Ό λ°ννλ©΄ μ λ¬λ κ°μ΄ Cat νμ
μ΄λΌλ λ»μ΄λ€.
return (animal as Cat).numberLives !== undefined;
}
function makeNoise(animal: Cat | Dog): string{
if(isCat(animal)){
return "Meow"
}else{
return "wowo"
}
}
νμ
νλ³μ λμλλ ν¨ν΄μΈλ° 곡ν΅μ μΈ νλ‘νΌν°λ₯Ό 곡μ νλ μ¬λ¬ μ νμ μμ±νκ³ ν΄λΉ νλ‘νΌν°κ° 리ν°λ΄ νμ
μ¦ λ¦¬ν°λ΄ κ°μ΄ λλ κ²μ΄λ€.
λͺ¨λ νμ
μ 곡ν΅λ νλ‘νΌν°μ νλ³μλ₯Ό μΆκ°νλ€.
interface Rooster{
name : string;
weight: number;
age: number;
kind: "rooster"; // νλ³μ νλ‘νΌν°
}
interface Cow{
name : string;
weight: number;
age: number;
kind: "cow"; // νλ³μ νλ‘νΌν°
}
interface Pig{
name : string;
weight: number;
age: number;
kind: "pig"; // νλ³μ νλ‘νΌν°
}
type FarmAnimal = Pig | Rooster | Cow;
function getFarmAnimalSound(animal: FarmAnimal){
switch(animal.kind){
case("pig"):
return "Oink"
case("cow"):
return "Moooo"
case("rooster"):
return "Cockadoodle"
}
}
μ€λ₯ λ° μνμ½λλ₯Ό νμ€νκ² μ²λ¦¬ν λ μ¬μ©νλ€.
never νμ
μ μ΄λλ ν λΉλμ§λ§ neverμλ μ΄λ€ νμ
λ ν λΉν μ μλ€
λ°μ μ½λλ neverκ° μμ΄μ defaultλ μ λ trueκ° λλ©΄ μλλ€.
interface Rooster{
name : string;
weight: number;
age: number;
kind: "rooster"; // νλ³μ νλ‘νΌν°
}
interface Cow{
name : string;
weight: number;
age: number;
kind: "cow"; // νλ³μ νλ‘νΌν°
}
interface Pig{
name : string;
weight: number;
age: number;
kind: "pig"; // νλ³μ νλ‘νΌν°
}
interface Sheep{
name: string;
weight: number;
age: number;
kind: "sheep";
}
type FarmAnimal = Pig | Rooster | Cow | Sheep;
function getFarmAnimalSound(animal: FarmAnimal){
switch(animal.kind){
case("pig"):
return "Oink"
case("cow"):
return "Moooo"
case("rooster"):
return "Cockadoodle"
default:
// neverνμ
μ΄ μλλ° defaultκ° trueκ° λμ΄μ μ€λ₯κ° λ°μνλ€.
const _exhaustiveCheck: never = animal; // error : Type 'Sheep' is not assignable to type 'never'
return _exhaustiveCheck;
}
}