νμ
μ€ν¬λ¦½νΈμλ κ΅μ₯ν λ§μ νμ
λ€μ΄ μ‘΄μ¬νκ³ μ€μ λ‘λ μμ£Ό μ¬μ©λμ§ μλ νμ
λ€λ λ§μ΄ μ‘΄μ¬ν©λλ€.
μλλ λ§μ κ°λ°μλ€μ΄ μ€λ¬΄μμ μ¬μ©νλ©΄μ κ°νΉ νΉμ μμ£Ό μ¬μ©νλ νμ
λ€ μ€ κΈ°λ³Έ νμ
λ€μ
λλ€.
λͺ κ°μ§λ₯Ό μ μΈνλ©΄ μλ°μ€ν¬λ¦½νΈμμλ μμ£Ό μ¬μ©νλ νμ λ€μ΄κΈ° λλ¬Έμ λ―μ€μ§ μμ μ½κ² μ¬μ©ν μ μμ΅λλ€ :)
Basic Types | |
type | desc. |
Number | λΆλ μμ κ°μ λ°μ΄ν°λ‘ κ°λ νμ |
Boolean | μ°Έ/κ±°μ§(true/false) κ°μ λ°μ΄ν°λ‘ κ°λ νμ |
String | ν μ€νΈ λ°μ΄ν° νμ |
Any | νμ κ²μ¬λ₯Ό νμ§ μκ³ , κ·Έ κ°λ€μ΄ μ»΄νμΌ μκ°μ κ²μ¬λ₯Ό ν΅κ³ΌνκΈΈ μν λ μ¬μ©νλ νμ (κΆμ₯ X) |
Object | μμ νμ μ΄ μλ νμ |
Array | κ°μ λ°°μ΄λ‘ μ¬μ©ν μ μκ² ν΄μ£Όλ νμ |
Unknown | μμ§ μ΄λ€ νμ μ΄ λ€μ΄μ¬μ§ λͺ¨λ₯Ό κ²½μ°, λ€μν νμ μ μ§μ΄λ£μ΄μΌν κ²½μ° μ¬μ©νλ νμ |
Union | OR(||) μ°μ°μμ λΉμ·ν μλ―Έλ₯Ό κ°μ§λ©° μ§μ λ νμ μ κ°λ§ μ¬μ© κ°λ₯ |
Conditional | μ‘°κ±΄λΆ νμ , νμ κ΄κ³ κ²μ¬λ‘ ννλ 쑰건μ λ°λΌ λ κ°μ§ κ°λ₯ν νμ μ€ νλλ₯Ό μ ν |
Type Alias | μλ‘μ΄ νμ μ μ μ, interfaceλ‘ ννν μ μκ±°λ μ λμ¨ λλ ννμ μ¬μ©ν΄μΌν λ μ¬μ© |
Interface βοΈ | ν¨μμ paramμ κ°μ²΄μ μμ± νμ μ μ μ ν μ μμ΅λλ€. μμμ ν΅ν΄ νμ₯μ΄ νμν λ μ¬μ© |
λΆλ μμ κ°μ λ°μ΄ν°λ‘ κ°λ νμ μ λλ€.
function addNumber(num1: number, num2: number) {
return num1 + num2;
}
addNumber(5, 10); // 15
μ°Έ/κ±°μ§(true/false) κ°μ λ°μ΄ν°λ‘ κ°λ νμ μ λλ€.
let isLogin: boolean = false;
if (isLogin) {
console.log('μ΄μμ€μΈμ! μ€λλ μ°Ύμμ£Όμ
μ κ°μ¬ν©λλ€ :)');
} else {
console.log('κ°μ
λ μ λ³΄κ° μμ΅λλ€, νμκ°μ
νμκ² μ΅λκΉ?');
}
ν μ€νΈ λ°μ΄ν° νμ μ λλ€.
function addString(str1: string, str2: string):string {
return str1 + str2;
}
addString("μλ
", "νμΈμ");
addString("hello", "world");
function welcomeMessage(name: string): string {
return `${name}λ μ€λλ 1commit ν΄λ΄μ!`;
}
console.log(welcome('Mobi'));
νμ
κ²μ¬λ₯Ό νμ§ μκ³ , κ·Έ κ°λ€μ΄ μ»΄νμΌ μκ°μ κ²μ¬λ₯Ό ν΅κ³ΌνκΈΈ μν λ μ¬μ©ν©λλ€.
any νμ
μ μ¬μ©μ νμ
μ€ν¬λ¦½νΈμ μλ―Έλ₯Ό ν΄μλκ² νλ μ‘΄μ¬λ‘ κΆμ₯νμ§ μμ΅λλ€.
let isLoggedIn: any = "λκ° μ€λ μ€λ₯ μ λ¨";
μμ νμ
μ΄ μλ νμ
μ μ¬μ©ν©λλ€.
μμ νμ
π number, string, boolean, bigint, symbol, null, λλ undefined
const userInfo: object = { id: "user-01", password: "12345" };
κ°μ λ°°μ΄λ‘ μ¬μ©ν μ μκ² ν΄μ£Όλ νμ μ λλ€.
type Mobi = {
userName: string,
gender: string,
term: number,
isOperator: boolean,
};
// 06-1. λ°°μ΄ μμλ€μ λνλ΄λ νμ
λ€μ []
let members: Mobi[] = [
{
userName: 'Kei',
gender: female,
term: 0,
isOperator: true,
},
{
userName: 'Jane',
gender: female,
term: 1,
isOperator: true,
},
{
userName: 'Heeppy',
gender: female,
term: 1,
isOperator: true,
},
];
// 06-2. μ λ€λ¦ λ°°μ΄ νμ
(Array<elementType>)
let members: Array<Mobi> = [
{
userName: 'Jack',
gender: male,
term: 2,
isOperator: false,
},
{
userName: 'Levi',
gender: male,
term: 2,
isOperator: false,
},
{
userName: 'Noel',
gender: male,
term: 2,
isOperator: false,
},
];
μμ§ μ΄λ€ νμ
μ΄ λ€μ΄μ¬μ§ λͺ¨λ₯Ό κ²½μ°, λ€μν νμ
μ μ§μ΄λ£μ΄μΌν κ²½μ° μ¬μ©ν©λλ€.
unknown νμ
μ λͺ¨λ μλ£ λ€ μ§μ΄λ£μ μ μμΌλ©° μλ£λ₯Ό λ£μ΄λ νμ
μ κ·Έλλ‘ unknownμ΄ μ μ§λ©λλ€.
anyμ λ€λ₯Έ μ μ unknown νμ
μλ μ무κ²λ ν μ μλ€λ κ²μ
λλ€.
anyλ μ΄λ€ νμ
μ΄ μ€λ κ°λ₯νλ€, unknownμ μ΄λ€ νμ
μ΄ μ¬μ§ λͺ¨λ₯΄κ² λ€μ λμμ€μ
λλ€.
let userID: unknown;
userID = true; // boolean κ° κ°λ₯
userID = 123; // number κ° κ°λ₯
userID = "hello"; // string κ° κ°λ₯
" μ anyλ κΆμ₯νμ§ μλλ° unknownμ λ³λ€λ₯Έ λ§μ΄ μλμ? "
anyλ λͺ¨λ νμ
κ³Ό μ°μ°μ νμ©νμ§λ§, unknownμ νμ
μ΄ λμ§ λͺ¨λ₯΄κΈ° λλ¬Έμ μλ¬΄λ° μ°μ°λ μννμ§ λͺ»ν©λλ€.
unknownμ anyμ λΉν΄ μμ μ μΈ νμ
μΌλ‘ μκ°ν μ μμΌλ©° unknownμΌλ‘ μ°μ°μ μννλ €λ©΄ λͺ
μμ μΌλ‘ νμ
μ νμΈν΄μΌ ν©λλ€.
OR(||) μ°μ°μμ λΉμ·ν μλ―Έλ₯Ό κ°μ§λ©° μ§μ λ νμ μ κ°λ§ μ¬μ© κ°λ₯ν©λλ€.
let age: string | number;
age = 20;
age = "Twenty";
function loginData(nickName: string, pw: string | number) {
// code ...
console.log(nickName, pw);
alert(`${nickName}λ, μ΄μμ€μΈμ!`);
}
loginData("Jack", "1234");
loginData("Levi", 4567);
type UXDesigner = {
nickName: string,
tool: string,
type: 'UX'
};
type UIDesigner = {
nickName: string,
tool: string,
type: 'UI'
};
type FEDeveloper = {
nickName: string,
language: string,
type: 'FE'
};
type BEDeveloper = {
nickName: string,
language: string,
type: 'BE'
};
function ClassifyOccupation(sb: UXDesigner | BEDeveloper) {
if ('language' in sb) {
// sb TDesigner λΌλ λ»
alert(`${sb}λμ κ°λ°μ μ’
μ¬μμ
λλ€.`)
}else{
alert(`${sb}λμ λμμ΄λ μ’
μ¬μμ
λλ€.`)
}
}
// typeofλ μμκ°λ§ κ²μ¬ κ°λ₯
function ClassifyDeveloper(sb: FEDeveloper | BEDeveloper) {
if (sb.type === "FE") {
alert(`${sb}λμ Frontend κ°λ°μ μ
λλ€.`)
}else{
alert(`${sb}λμ Backend κ°λ°μ μ
λλ€.`)
}
}
μ‘°κ±΄λΆ νμ
, νμ
κ΄κ³ κ²μ¬λ‘ ννλ 쑰건μ λ°λΌ λ κ°μ§ κ°λ₯ν νμ
μ€ νλλ₯Ό μ νν©λλ€.
μλ°μ€ν¬λ¦½νΈμμ μΌνμ°μ°μ κ°μ μν μ
λλ€.
T extends U ? X : Y π (Tκ° Uμ ν λΉ κ°λ₯νμ§μ λ°λΌμ Xμ Yμ€ νλμ νμ μ μ ν)
type IsString<T> = T extends string ? "yes" : "no";
type sample1 = IsString<"κ°λλ€λΌ">; // yes
type sample2 = IsString<1234>; //no
type sample3 = IsString<true>; //no
// 'T'μ 'U'κ° κ°μ νμ
μ΄λ©΄ 'Yes' νμ
μ, λ€λ₯Έ νμ
μ΄λ©΄ 'No' νμ
μ λ°ν
type IsSameType<T, U> = T extends U ? (U extends T ? "Yes" : "No") : "No";
type Result1 = IsSameType<string, string>; // 'Yes'
type Result2 = IsSameType<string, number>; // 'No'
type Result3 = IsSameType<"a" | "b", string>; // 'No'
// conditional typeμΌλ‘ formatted-date λ§λ€κΈ°
type DateFormat = 'YYYY-MM-DD' | 'MM/DD' | 'HH:mm';
type GetElements<T> = T extends 'Y'
? 'year'
: T extends 'M'
? 'month'
: T extends 'D'
? 'date'
: T extends 'H'
? 'hour'
: T extends 'm'
? 'minute'
: never;
type SplitDateReturn <Format extends DateFormat> = Format extends `${infer C}${infer Rest}`
? { [E in GetElements<C> | keyof SplitDateReturn<Rest>]: number }
declare function splitDate<Format extends DateFormat>(date: Date, format: Format):SplitDateReturn<Format>;
splitDate(newDate(), 'YYYY-MM-DD') // {year: number, month: number, date: number}
splitDate(newDate(), 'MM/DD') // {month: number, date: number}
splitDate(newDate(), 'HH:mm') // {hour: number, minute: number}
μλ‘μ΄ νμ
μ μ μνλ κ²μΌλ‘ interfaceμ μ μ¬ν νΉμ§μ κ°κ³ μμ΅λλ€.
interfaceλ‘ ννν μ μκ±°λ μ λμ¨ λλ ννμ μ¬μ©ν΄μΌν λ type aliasλ₯Ό μ¬μ©ν©λλ€.
μμ νμ
μ λ³μΉμ μ νλ κ²μ κΆμ₯νμ§ μλ μ¬νμ
λλ€.
// μμ νμ
μ λ³μΉμ μ νλ κ²μ κΆμ₯ X
type Name = string;
type Age = number;
const mobi: Name = "Peanut";
const age: Age = 20;
// κ°μ²΄μ λ³μΉμ£ΌκΈ°
type MobiMember = {
name: string;
nickName: string;
term: number;
isOperator: boolean;
};
const Amy:MobiMember = {
name: "μ€μ£Όμ°",
nickName: "Amy",
term: 2,
isOperator: true,
};
ν¨μμ paramμ κ°μ²΄μ μμ± νμ μ μ μ ν μ μμ΅λλ€. μμμ ν΅ν΄ νμ₯μ΄ νμν λ μ¬μ©ν©λλ€.
interface Peanut {
name: string;
nickName: string;
age: number;
isDirector: boolean;
}
function printName(person: Peanut) {
console.log(person.name); // "κΉμ±μ©"
console.log(person.nickName); // "Peanut"
console.log(person.age); // 20
console.log(person.isDirector); // true
}
interface Developer {
isMobi: boolean;
nickName: string;
}
//μΈν°νμ΄μ€ νμ₯
interface whichDev extends Developer {
sort: string;
}
const Zero: Developer = {
isMobi: true;
nickName: 'Zero';
};
const Zoey: Developer = {
isMobi: true;
nickName: 'Zoey';
};
const Peanut: whichDev = {
isMobi: true;
nickName: 'Peanut';
sort: 'Full';
};
const Rin: whichDev = {
isMobi: true;
nickName: 'Rin';
sort: 'FE';
};
μλμ κ°μ΄ keyκ° stringμΈ κ²½μ°μ νμ λͺ¨λ μμ±λ€μ λ€ νμ©ν μλ μμ΅λλ€.
μΈλ±μ€ μκ·Έλμ²λ λͺ¨λ μμ±λ€μ λ€ μ μν μ μμ λ μ μ©νκ² μ°μΌ μ μμ΅λλ€.
interface MobiInfo {
userName: string,
nickName: string,
name: string,
term: number,
isOperator: boolean,
[key: string]: any; // index signiture
}
const Peter: MobiInfo = {
userName: 'Peter',
nickName: 'Peanut',
name: 'κΉμ±μ©',
term: 0,
isOperator: true,
isDirector: true, // index signiture
hobby: 'coding', // index signiture
MBTI: 'ENTP', // index signiture
}
const Daniel: MobiInfo = {
userName: 'Daniel',
nickName: 'λ€λ
€',
name: 'μ΄λκ²½',
term: 2,
isOperator: false,
hobby: 'soccer', // index signiture
MBTI: 'ENTJ', // index signiture
}
const Kimi: MobiInfo = {
userName: 'Kimi',
nickName: 'ν΄λ―Έ,
name: 'κΉκ°μ',
term: 2,
isOperator: false,
}
νμ μ μ£Όμ΄μΌνλ κ²½μ°μ μ£Όμ§ μμλ λλ κ²½μ°λ₯Ό ꡬλΆνκΈ°
β¬οΈ javascript
let seongyong = {
age: 20,
height: 190,
};
function log(obj) {
console.log(obj.height);
return obj;
}
const a = log(seongyong);
console.log(a.age);
β¬οΈ typescript
interface Person = {
age: number,
height: number,
};
let seongyong:Person = {
age: 20,
height: 190
}
맀κ°λ³μμ νμ κ³Ό λ¦¬ν΄ κ°μ νμ μ΄ λμΌν κ²½μ° π TS μ ν
function log(obj: Person) {
console.log(obj.height);
return obj;
}
λ¦¬ν΄ κ°μ νμ
μ 맀κ°λ³μμ νμ
λ§μΌλ‘λ μΆλ‘ μ΄ μ λλ κ²½μ° π TS νμ
ex. νμ
ν©μ±
interface Movvi = {
nickname: string,
isDirector: boolean,
}
function log(obj: Person):Person & Movvi{
return {...obj, nickname : 'Peanut', isDirector: true}
}
νμ
μ΄ μΆλ‘ μ΄ μΆ©λΆν κ°λ₯ν κ²½μ°(맀κ°λ³μμ λ¦¬ν΄ κ°μ νμ
μ΄ λμΌν κ²½μ°) νμ
μ μ£Όλ κ²μ κ°λ°μλ§λ€ μκ°μ΄ λ€λ₯Ό κ²μ
λλ€.
νμ
μ λΆμ¬νκ² λλ©΄ λΆλμ΄νκ² μ½λμ κΈΈμ΄κ° κΈΈμ΄μ Έ κ°λ
μ±μ΄ λ¨μ΄μ§λ€ μκ°λ μλ μκ³
μΈμ΄ μλΉμ€λ₯Ό μΆ©λΆν μ§μλ°μ μ μκΈ° λλ¬Έμ κ΅³μ΄ μ°μ§ μμλ λλ€ μκ°ν μ μμ΅λλ€.
λ°λλ‘ μ½λμ 컨벀μ
μ΄λ νμ
μ μν΄ μΆλ‘ μ΄ κ°λ₯ν κ²½μ°μλ νμ
μ μ£Όλ κ²μ μ νΈν μλ μμ΅λλ€.
νμ
νλ κ°λ°μκ° μ΄λ€ μ±ν₯μ΄λμ λ°λΌ νλ‘μ νΈμ ν¬κΈ°μ λ°λΌ λ¬λΌμ§ μλ μμ΅λλ€.
νμ¬ μ λ νμ
μ€ν¬λ¦½νΈλ₯Ό λ°°μ΄μ§ μΌμ£ΌμΌ μ λ λ°μ λμ§ μμκΈ° λλ¬Έμ νμ
μ μ£Όλ κ²μ΄ λ«λ€κ³ μκ°ν©λλ€.
λ°νμμ νμ
μ΄ μ μ λλ€λ μ μ μκ°ν΄λ΄€μ λ μμ§μ μ΅λν μμΈν μ£Όλ κ²μ΄ μ€λ₯λ₯Ό μ΅μνν λΏλ§ μλλΌ
νμ
μ¬μ©μ μ΅μν΄μ§λ κΈΈμ΄λΌκ³ μκ°λ©λλ€.
λ°λΌμ νμ
μ μλ΅μ νμ
μ€ν¬λ¦½νΈλ₯Ό 곡λΆνκ³ μμ λ§μ΄ μ΅μλ€κ³ μκ°λ λ νλ κ²μ΄ μ’μ κ² κ°μ΅λλ€ :)