in
μ μ¬μ©νμ¬ μ¬μ©μ μ μ νμ
μ ꡬλΆνμ¬ μμ±ν μ μλ€.instanceof
λ₯Ό μ¬μ©νμ¬ κ΅¬λΆν΄μ€λ€.type Combinable = string | number;
type Numeric = number | boolean;
type Universal = Combinable & Numeric;
function add(n1: Combinable, n2: Combinable) {
if (typeof n1 === "string" || typeof n2 === "string") {
return n1.toString() + n2.toString();
}
return n1 + n2;
}
ππ» if (typeof n1 === "string" || typeof n2 === "string")
μ΄ λ¬Έμ νμ
κ°λλΌκ³ νλ€.
ππ» νμ§λ§ typeofμΌλ‘λ μ¬μ©μ μ μ νμ
μ ꡬλΆν μ μλ€.
type UnknownEmployee = Employee | Admin;
function printEmployeeInformation(emp: UnknownEmployee) {
if ("privileges" in Employee) {
// νμ
κ°λ
console.log("privileges: " + emp.privileges);
}
if ("startDate" in Admin) {
// νμ
κ°λ
console.log("startDate: " + Admin.startDate);
}
}
typeof ν΄λμ€
λ‘ νμΈν μ μλ€.class A { aaa(){} }
class B { bbb(){} }
function aorb(param: A | B){
if(param instanceof A){
param.aaa();
}
}
aorb(new A());
aorb(new B());
interface Bird {
type: "bird";
flyingSpeed: number;
}
interface Horse {
type: "horse";
runningSpeed: number;
}
type Animal = Bird | Horse;
function moveAnimal(animal: Animal) {
switch (animal.type) {
// μλμμ±μΌλ‘ ν΅ν΄ μ νν ꡬλ³μ ν μ μμ.
case "bird": {
console.log(animal.flyingSpeed);
break;
}
case "horse": {
console.log(animal.runningSpeed);
break;
}
}
}
moveAnimal({ type: "bird", flyingSpeed: 200 });
// moveAnimal({type:'bird',runningSpeed:100 }); errorλ°μ
const inputEl = document.getElementById("input-field");
inputEl.value = "hello"; // errorλ°μ
ππ» valueκ° HTMLElementνμ
μ μ‘΄μ¬νμ§ μλ errorκ° λ°μνλ€.
ππ» κΈ°λ³Έμ μΌλ‘ λͺ¨λ HTML μμκ° νμ
μΌλ‘μ κ°λ μ λ€λ¦ νμ
μ΄ νΉμ HTMlμμμΈ μμ±μ μ§μνμ§ μλλ€.
ππ» λ°λΌμ inputElκ° λ¨μν nullμ΄ μλ HTML inputEelement νμ
μμ TSμκ² μλ €μΌ νλ€. μ΄λ νλ³νμ μ¬μ©νλ€.
: λ³ννκ³ μνλ μμ μμ΄λ νμ μ€ν¬λ¦½νΈμ νμ μ μλ €μ£Όκ³ μ νλ μμΉ μμ νμ μ μΆκ°νλ€.
const inputEl = <HTMLInputElement>document.getElementById("input-field");
inputEl.value = "hello"; // errorλ°μ
ππ» <HTMLInputElement>
μ νμ¬ μ¬μ© μ€μΈ νμ
μ λν μ 보λ₯Ό μ λ¬νκΈ° μν΄ μ¬μ©νλκ² μλ λΉλ λꡬμ 리μ‘νΈλ‘ ꡬ문 λΆμλμ΄ κ²°κ³Όμ μΌλ‘ νλ©΄μ λ λλ§νκ³ μ νλκ² λ¬΄μμΈμ§ μμλ΄λλ° μ¬μ©νλ€.
const inputEl = document.getElementById("input-field")! as HTMLInputElement;
inputEl.value = "hello";
ππ» jsxꡬ문과 ꡬλ³νκΈ° μν΄ asν€μλλ₯Ό μ¬μ©νλ€.
ππ» document.getElementById("input-field")!
λ HTMLInputElementνμ
μ κ°μ λ°ννλ―λ‘ μλ¬λ₯Ό λ°μμν€μ§ μλλ€.
πΎ λλνμ λμ
const inputEl = document.getElementById("input-field");
if (inputEl) {
// ννμμ κ²°κ³Όμ λν κ°μ μ κ·Όν΄μΌ νλ€.
(inputEl as HTMLInputElement).value = "hello";
}
[prop: type]: type
πΎ μ¬μ©μ μ λ ₯μ μ ν¨μ± κ²μ¬λ₯Ό νλ κ²½μ°
interface ErrorContainer {
// λ°λμ λ¬Έμμ΄μ ν€, κ°μ μ§λ
μΌ νλ€.
[prop: string]: string; // μΈλ±μ€ νμ
μ μ€μ !!
// [prop: number]: string;
}
const errorBag: ErrorContainer = {
// 1: "Not a valid email", ππ» 1λ λ¬Έμμ΄λ‘ ν΄μλκΈ° λλ¬Έμ λ¬Έμ μλ€.
email: "Not a valid email",
username: "Not a valid username",
};
πΎ #01
type Combinable = string | number;
function add(n1: Combinable, n2: Combinable) {
if (typeof n1 === "string" || typeof n2 === "string") {
return n1.toString() + n2.toString();
}
return n1 + n2;
}
const result = add("Max", " Siri");
// μ΄λ λ¬Έμμ΄μ΄ λ°νλλ€λ κ²μ TSλ μμ§ λͺ»ν΄ λ¬Έμλ©μλμ¬μ©μ νμ§λͺ»νλ€.
result.split(" "); // error
ππ» const result = add('Max',' Siri') as string;
: as stringμΌλ‘ νλ³νμ λͺ
μμ μΌλ‘ μμ±νλ©΄ λμ§λ§ μ½λκ° κΈΈμ΄μ§λ€λ λ¨μ μ΄ μλ€.
ππ» μ΄λ ν¨μ μ€λ²λΌμ΄λλ₯Ό μ¬μ©νλ©΄ λλ€.
πΎ #02 - ν¨μ μ€λ²λΌμ΄λ μ¬μ©
type Combinable = string | number;
function add(n1: number, n2: number): number;
function add(n1: string, n2: string): string;
function add(n1: number, n2: string): string;
function add(n1: string, n2: number): string;
function add(n1: Combinable, n2: Combinable) {
if (typeof n1 === "string" || typeof n2 === "string") {
return n1.toString() + n2.toString();
}
return n1 + n2;
}
const result = add("Max", " Siri");
result.split(" "); // λ¬Έμμ΄μ λ°ννλκ²μ μΈμν¨
πΎ #01
: κ°μ²΄ λ°μ΄ν°μ μ€μ²©λ μμ±κ³Ό κ°μ²΄μ μμ νκ² μ κ·Όν μ μκ²νλ€.
: λ°±μλμμ λ°μ΄ν°λ₯Ό κ°μ Έμ¬ κ²½μ°, μΌλΆ λ°μ΄ν°κ° κ°λ°μκ° μμ±νλλ‘ λΆλ¬μμ§μ§ μμμ λ μ νμ 체μ΄λμ μ¬μ©νμ¬ μλ¬λ₯Ό λ°©μ§νλ€.
const fetchedUserData = {
id: "u1",
name: "job",
// job: { title: "CEO", description: "My own company"}
};
console.log(fetchedUserData?.job.title); // error
??
: null, undefunedμΌ κ²½μΈ ν΄λ°±κ°μ μ μ₯νλ€.const a = null;
console.log(a ?? "DEFAULT"); // DEFAULT