Typescript ๊ธฐ์ด2
Typescript ๊ธฐ์ด2 ๐
Literal Type
let ์ซ์: 123;
์ซ์: 456; // ERROR
์ ํด์ง ๊ฐ๋ง ๋ค์ด์ค๊ฒ ํ์ ์ ์ ์ธํ ์ ์๋ค.
let a = (x: '๊ฐ์' | '๋ฐ์' | '๋ณด'): ('๊ฐ์' | '๋ฐ์' | '๋ณด')[] => {
return ['๊ฐ์', '๋ณด'];
};
ํจ์๋ ๊ฐ๋ฅ
var ์๋ฃ = {
name: 'kim',
};
function ๋ดํจ์(a: 'kim') {}
๋ดํจ์(์๋ฃ.name); // ERROR
์๋ฌ๊ฐ ๋จ๋ ์ด์ ๋ a: 'kim'
์ ๋ป์ โkimโ์ด๋ผ๋ type๋ง ๋ฐ๊ฒ ๋ค๋๊ฑฐ๋ค.
๋ฐ๋ผ์ ๊ฐ์ด โkimโ์ธ ์๋ฃ.name์ ๋ฃ์ผ๋ฉด ์๋ฌ๊ฐ ๋ฌ๋ค.
var ์๋ฃ = {
name: 'kim',
} as const;
function ๋ดํจ์(a: 'kim') {}
๋ดํจ์(์๋ฃ.name); // ERROR
์ด๋ ๊ฒ as const๋ฅผ ์ฌ์ฉํ๋ฉด Literal Type์ฒ๋ผ ์ง์ ํด์ฃผ๋๋ฐ
name: โkimโ ์ด๋ ๊ฒ ํ์ ์ด ์ง์ ๋๋ค.
- ํค๊ฐ์ ํ์ ์ผ๋ก ์ง์ ํด์ค
- ํค๋ฅผ readonly๋ก ์ง์ ํ์ฌ ๊ฐ์ด ๋ณํ์ง ์๊ฒํด์ค
methods ์์ ํ์ ์ง์ ํ๊ธฐ
type Member = {
name: string;
age: number;
plusOne: (x: number) => number;
changeName: () => void;
};
let ํ์์ ๋ณด: Member = {
name: 'kim',
age: 30,
plusOne(x) {
return x + 1;
},
changeName: () => {
console.log('์๋
');
},
};
ํ์์ ๋ณด.plusOne(1);
ํ์์ ๋ณด.changeName();
๋ฉ์๋์ ํ์ ์ง์
type CutType = (x: string) => string;
type RemoveDash = (x: string) => number;
let cutZero: CutType = (x) => {
let result = x.replace(/^0+/, '');
return result;
};
let removeDash: RemoveDash = (x) => {
let result = x.replace(/-/g, '');
return parseFloat(result);
};
function allFunction(a: string, func1: CutType, func2: RemoveDash): void {
let result = func1(a);
let result2 = func2(result);
console.log(result2);
}
allFunction('010-1111-2222', cutZero, removeDash);
class
class Person {
name: string; // ํ๋๊ฐ
constructor(a: string) {
this.name = a;
}
}
ํ๋๊ฐ์ ๋ฏธ๋ฆฌ ์ ์ธํด์ฃผ์ด์ผ ์ฌ์ฉ๊ฐ๋ฅ
class Word {
num;
str;
constructor(...param) {
let ์ซ์๋ค: number[] = [];
let ๋ฌธ์๋ค: string[] = [];
param.forEach((i) => {
if (typeof i === 'string') {
๋ฌธ์๋ค.push(i);
} else {
์ซ์๋ค.push(i);
}
});
this.num = ์ซ์๋ค;
this.str = ๋ฌธ์๋ค;
}
}
let obj = new Word('kim', 3, 5, 'park');
console.log(obj.num); //[3,5]
console.log(obj.str); //['kim', 'park']
์์
interface
interface Square {
color: string;
width: number;
}
//type Square = { color: string; width: number };
let square: Square = { color: 'red', width: 100 };
object ํ์ ์ง์ ์ interface ์ฌ์ฉ๊ฐ๋ฅ
interface Student {
name: string;
}
interface Teacher {
name: string;
age: number;
}
//--------------------------------------------------------------------------------name ๊ฐ์ผ๋ฏ๋ก ํฉ์น๋ค
interface Student {
name: string;
}
interface Teacher extends Student {
age: number;
}
class ๋ฌธ๋ฒ๊ณผ ๋น์ทํ๊ฒ ์ฌ์ฉ๊ฐ๋ฅ
type Student = { name: string };
type Teacher = { name: string; age: number };
//--------------------------------------------------------------------------------name ๊ฐ์ผ๋ฏ๋ก ํฉ์น๋ค
type Student = { name: string };
type Teacher = { age: number } & Student;
type๋ ์์๊ฐ์ด ์ฌ์ฉ๊ฐ๋ฅ
interface Cart {
product: string;
price: number;
}
let ์ฅ๋ฐ๊ตฌ๋: Cart[] = [
{ product: '์ฒญ์๊ธฐ', price: 7000 },
{ product: '์ผ๋ค์', price: 800 },
];
๋ฐฐ์ด์์ ๊ฐ์ฒด๋ง๋ค๊ธฐ
interface MathObj {
plus: (a: number, b: number) => number;
minus: (a: number, b: number) => number;
}
let ์ค๋ธ์ ํธ: MathObj = {
plus(a, b) {
return a + b;
},
minus(a, b) {
return a - b;
},
};
interface ๋ฉ์๋
Interface vs Type
Interface๋ ์ค๋ณต์ ์ธ ๊ฐ๋ฅ Type ์ค๋ณต์ ์ธ ๋ถ๊ฐ๋ฅ
interface Student {
name: string;
}
interface Student {
age: number;
}
์ด๋ ๊ฒ ์ค๋ณต์ ์ธํ๋ฉด
type Student = { name: string, age: number}๊ณผ ๊ฐ๋ค
์ฆ, ์๋์ผ๋ก extendsํด์ค๋ค.