π νμ μ€ν¬λ¦½νΈλ₯Ό νμ΅νλ©΄μ λ°°μ΄ λ΄μ©λ€μ λΈλ‘κ·Έμ κΈ°λ‘ν©λλ€ (feat. λλ¦Όμ½λ©)
const num: number = -6; // number
const str: string = 'hello'; // string
const boal: boolean = false; // boolean
/* undefined */
let name: undefined; // π© μ΄λ°μμΌλ‘ μ μ°μ΄μ§ μμ
let age: number | undefined // numberνμ
νΉμ undefined
age = undefined;
age = 1;
/* null */
let person: null; // π©
let person: string | null;
/* unknown : π© λͺ¨λ νμ
μ΄ νμ©λλ―λ‘ μ μ°μ΄μ§ μμ */
let notSure: unknown = 0;
notSure = 'he';
notSure = true;
/* any : π© λͺ¨λ νμ
μ΄ νμ©λλ―λ‘ μ μ°μ΄μ§ μμ */
let anything: any = 0;
anything = 'hello';
/* void : μ무κ²λ 리ν΄νμ§ μλ ν¨μμ νμ
(μλ΅ κ°λ₯) */
function print(): void {
console.log('hello');
return;
}
let unusable: void = undefined; // π©
/* never : μ λ 리ν΄λμ§ μλ ν¨μμ λν΄ λͺ
μνκΈ° μν΄ μ°μ (μλ¬, 무ν루ν λ±) */
function throwError(message: string): never {
throw new Error(message);
// while (true) {}
}
/* object : function, array λͺ¨λ objectμ ν¬ν¨λλ―λ‘ λ³΄λ€ λ μ ννκ² λͺ
μν΄μΌν¨ */
let obj: object; //π©
function acceptSomeObject(obj: object) {}
acceptSomeObject({ name : 'Mason' })
function add(num1: number, num2: number): number {
return num1 + num2;
}
/* Promise 리ν΄νλ κ²½μ° */
function fetchNum(id: string): Promise<number> {
// fetch code...
return new Promise((resolve, reject) => {
resolve(100);
});
}
function printName(firstName: string, lastName?: string) {
console.log(firstName);
console.log(lastName);
}
printName('Steve', 'Jobs'); // 'Steve', 'Jobs'
printName('Mason'); // 'Mason', 'undefined'
function printMessage(message: string = 'default message') {
console.log(message);
}
printMessage(); // 'default message'
function addNumbers(...numbers: number[]): number {
return numbers.reduce((a, b) => a + b);
}
addNumbers(1, 2); // 3
addNumbers(1, 2, 3, 4, 5); // 15
// 첫 λ²μ§Έ λ°©λ² - elementType[]
const fruits: string[] = ['apple', 'banana'];
// λ λ²μ§Έ λ°©λ² - Array<elementType>
const scores: Array<number> = [1, 3, 4];
// immutable λ°°μ΄ μΈμλ‘ λ°κΈ°
function printArray(fruits: readonly string[]) {}
let student: [string, number];
student = ['name', 123];
student = [123, 'name']; // Error!
student[0]; // name;
student[1]; // 123
const [name, age] = student;
// Tupleμ μΈλ±μ€λ‘ μ κ·Όν΄μΌ νλ―λ‘ κ°λ
μ±μ΄ λ¨μ΄μ§λ νΈ..
// μ£Όλ‘ interface, type alias, class λ°©μμΌλ‘ λ체νμ¬ μ¬μ©
type Text = string; // Text λΌλ νμ
μ μμ±
const name: Text = 'Mason' // name λ³μμ Text νμ
μ μ©
// κ°μ²΄μ type alias μ μ©νκΈ°
type Student = {
name: string,
age: number
}
const me: Student {
name: 'Mason',
age: 29
}
type JSON = 'json';
const json: JSON = 'json';
const json: JSON = 'others' // Error!
μ¬λ¬ κ°μ νμ μΌλ‘ μ§μ ν μ μμ ( Union Types )
νμ©λκ° λλ€ π
type Direction = 'left' | 'right' | 'up' | 'down';
function move(direction: Direction) {
console.log(direction);
}
move('down');
type TileSize = 8 | 16 | 32;
const tile: TileSize = 16;
// Discriminated Union
// λμΌν ν€(result), λ€λ₯Έ κ°μ κ°μ§κ² ν¨
// -> printLoginState ν¨μ λ΄μμ state.result μ μ κ·Όν μ μμ
type SuccessState = {
result:'success';
response: string;
}
type failState = {
result: 'fail';
reason: string;
}
type LoginState = SuccessState | FailState;
function login(): LoginState {
return {
result: 'success',
response: {
body: 'logged in!'
}
};
}
function <printLoginState(state: LoginState) {
if(state.result) {
console.log(`π ${state.response.body}`);
} else {
console.log(`π ${state.reason}`)
}
}
type Student = {
name: string;
score: number;
}
type Worker = {
employeeId: number;
work: () => void;
}
function internWork(person: Student & Worker) {
console.log(person.name, person.employeeId, person.work());
}
internWork({
name: 'mason',
score: 1,
employeeId: 123,
work: () => {}
})
// JavaScript
const MAX_NUM = 6;
const MAX_STUDENTS_PER_CLASS = 10;
const DAYS_ENUM = Object.freeze({"MONDAY": 0, "TUESDAY": 1, "WEDNESDAY": 2});
const dayOfToday = DAYS_ENUM.MONDAY;
// TypeScript
enum Days {
Monday, // enumμ κ°μ ν λΉ ν μ μλ€. ex) Monday = 'mon'
Tuesday,
Wednesday,
}
// κ°μ΄ ν λΉ λμ§ μμΌλ©΄ 0λΆν° μ°¨λ‘λλ‘ ν λΉλλ€
let day = Days.Monday
console.log(day) // -> 0 ..
// enumμ κ°μ΄ ν λΉλ λ€ λ€λ₯Έ κ°μ ν λΉν μ μλ€ (νμ
μ΄ μ ννκ² λ³΄μ₯λμ§ μμ)
day = 10
// λ°λΌμ νμ
μ€ν¬λ¦½νΈμμ enumμ κ°κΈμ μ°μ§ μλ κ²μ΄ μ’μΌλ©° Union νμ
μΌλ‘ λ체 κ°λ₯νλ€
type dayOfWeek = 'Monday' | 'Tuesday' | 'Wednesday'
let text = 'hello';
text = 1 // Error!
// μΈμμ νμ
μ λͺ
μνμ§ μμΌλ©΄ anyλ‘ μΆλ‘
function print(message = 'hello') {
console.log(message);
}
print('hello');
print(1);
function add(x: number, y: number) {
return x + y;
}
// λ¦¬ν΄ νμ
μ λͺ
μνμ§ μμλ numberλ‘ μΆλ‘
const result = add(1, 2) // type: number
κ·Έλλ νμ μ μ ννκ² λͺ μν΄ μ£Όλκ² μ’λ€ π
function jsStrFunc(): any {
return 2;
}
const result = jsStrFunc();
console.log((result as string).length);
console.log((<string>result).length);
const wrong: any = 5;
console.log((wrong as Array<number>).push(1)); // π±
function findNumbers(): number[] | undefined {
return undefined;
}
const numbers = findNumbers()!;
numbers.push(2); // π±
const button = document.querySelector('class')!;