πΎ role
let person: {
name: string;
age: number;
role: [number, string];
} = {
name: 'zooyaho',
age: 25,
role: [2, 'admin'],
}
person.role.push('author'); // errorλ₯Ό λ°μνμ§ μμ!
πΎ
: roleμ idλ κ°κ° κ΄λ¦¬μλ 0, μ½κΈ° μ μ© μ¬μ©μλ 1, μμ±μλ 2
// 0, 1, 2κ° κ°κ° ν λΉλ¨
enum Role { ADMIN, READ_ONLY, AUTHOR };
/*
enum Role { ADMIN = 5, READ_ONLY, AUTHOR };
-> AUTHORμ κ°κ° 5, 6, 7μ΄ ν λΉλ¨
*/
let person = {
name: 'zooyaho',
age: 25,
role: Role.ADMIN,
}
if(person.role === Role.ADMIN) {
console.log("is Admin!!");
}
type Combinable = number | string;
function combine(input1: Combinable, input2: Combinable){...}
:
μ μ¬μ©νμ¬ ν¨μ λ°ννμ
μ μ μν μ μλ€.:void
λ₯Ό μ¬μ©ν΄μΌ νλ€.returnμ΄ μλ κ²
κ³Ό return undefined
μ νμ
μ§μ μ λ€λ₯΄λ€. returnμ΄ μλ κ²
| return;
μ :void
λ‘, return undefined;
μ :undefined
μΌλ‘ μ νν λͺ
μν΄μΌ νλ€.let fn : Function
μΌλ‘ μλ³μμ Functionνμ
μ μ§μ νλ€.πΎ Functionνμ μ§μ
function add(n1:number, n2:number){...}
let combineFn: Function;
combineFn = 5; // error
combineFn = add;
πΎ νμ΄νν¨μ νκΈ°λ²μΌλ‘ ν¨μνμ μ§μ
// 맀κ°λ³μλ₯Ό μ·¨νμ§ μλ numberλ₯Ό λ°ννλ ν¨μ νμ
// let combineFn: () => number;
let combineFn: ( a:number, b:number ) => number;
combineFn = 5; // error
combineFn = add;
function addAndHandle(n1:number, n2:number, cb: (num: number) => void) {
const result = n1 + n2;
cb(result);
}
addAndHandle(2, 3, (result) => {
console.log(result);
})
/*
addAndHandle(2, 3, (result) => {
console.log(result);
return result; // errorκ° λ°μνμ§ μμ!!!
})
ππ» return μμ
μ΄ λ¬΄μλλ€!!
*/
let userInput: unknown;
let userName: string;
userInput = 5;
userInput = 'Max';
userName = userInput; // error
ππ» unknownμ stringμΌλ‘ μΈμλμ§ μμ errorκ° λ°μλλ€!
ππ» unknownμ anyλ‘ λ³κ²½νλ©΄ errorκ° λ°μλμ§ μλλ€.
πΎ μΆκ° μ μΈ νμ κ²μ¬λ₯Ό ν΅ν΄ ν λΉν μ μλ€.
let userInput: unknown;
let userName: string;
if(typeOf userInput === 'string') {
userName = userInput;
}
function generateError(message: string, code: number){
throw { message: message, errorCode: code }
}
generateError('μλ¬ λ°μ', 500);
ππ» μ΄ ν¨μλ neverλ₯Ό λ°ννλ©° λ°ν κ°μ μμ±νμ§ μκΈ°λλ¬Έμ void ν¨μμ΄κΈ°λ νλ€.