ν°μ€ν 리 λ·°
π Interfaces
Interfacesλ μ΄λ€ νμ μ λ§λλ λ°©μμΈλ° λ΄λΆμ μΈ κ²κ³Ό μκ΄μμ΄ μΈλΆμ μΌλ‘ λλ¬λλ κ°μ²΄μ μ¬μ©λ°©μμ΄ μ ν νμ μ΄λ€.
function hello(person: { name: string; age: number; }): void {
console.log(`μλ
νμΈμ! ${person.name} μ
λλ€.`);
}
const p: { name: string; age: number; } = {
name: 'Mark',
age: 35
};
hello(p); // μλ
νμΈμ! Mark μ
λλ€.
//Interfaces μ¬μ©νλ©΄
interface Person {
name: string;
age: number;
}
function hello(person: Person): void {
console.log(`μλ
νμΈμ! ${person.name} μ
λλ€.`);
}
const p: Person = {
name: 'Mark',
age: 35
};
hello(p); // μλ
νμΈμ! Mark μ
λλ€.
β optional property
μν©μ λ°λΌ μμ μλ μμ μλ μλ optional property
ν¬κ² λ κ°μ§ λ°©λ²μ΄ μλλ° μ²«λ²μ§Έ λ°©λ²μ λ€μκ³Ό κ°μ΄ ?λ₯Ό λ£λ κ²μ΄λ€.
interface Person {
name: string;
age?: number;
}
function hello(person: Person): void {
console.log(`μλ
νμΈμ! ${person.name} μ
λλ€.`);
}
const p1: Person = {
name: 'Mark',
age: 35
};
const p2: Person = {
name: 'Anna'
};
hello(p1); // μλ
νμΈμ! Mark μ
λλ€.
hello(p2); // μλ
νμΈμ! Anna μ
λλ€.
?λ£λ λ°©λ²μ΄ μλ λ λ²μ§Έ λ°©λ²μ Indexable typesμ΄λ€.
μ΄λ€ μ΄λ¦μ νλ‘νΌν°κ° μλ μκ΄μλ€.
interface Person {
name: string;
age?: number;
[index: string]: any;
}
function hello(person: Person): void {
console.log(`μλ
νμΈμ! ${person.name} μ
λλ€.`);
}
const p1: Person = {
name: 'Mark',
age: 35,
};
const p2: Person = {
name: 'Anna',
systers: ['Joy', 'Hani']
};
const p3: Person = {
name: 'Bokdaengi',
father: p1,
mother: p2
};
hello(p1); // μλ
νμΈμ! Mark μ
λλ€.
hello(p2); // μλ
νμΈμ! Anna μ
λλ€.
hello(p3); // μλ
νμΈμ! Bokdaengi μ
λλ€.
π§‘ function in interface
interface Person {
name: string;
age: number;
hello(): void;
}
const p1: Person = {
name: 'Mark',
age: 35,
hello: function (): void {
console.log(this);
console.log(`μλ
νμΈμ! ${this.name} μ
λλ€.`);
}
};
const p2: Person = {
name: 'Mark',
age: 35,
hello(): void {
console.log(this);
console.log(`μλ
νμΈμ! ${this.name} μ
λλ€.`);
}
};
const p3: Person = {
name: 'Mark',
age: 35,
hello: (): void => {
console.log(this);
console.log(`μλ
νμΈμ! ${this.name} μ
λλ€.`);//this μλ¬
}
};
p1.hello(); // μλ
νμΈμ! Mark μ
λλ€.
p2.hello(); // μλ
νμΈμ! Mark μ
λλ€.
p3.hello(); // μλ
νμΈμ! μ
λλ€.
π class implements interface
interfaceλ₯Ό μ΄μ©ν΄ classλ₯Ό λ§λ€ μ μλ€.
interface IPerson {
name: string;
age?: number;
hello(): void;
};
class Person implements IPerson {
name: string;
constructor(name: string) { //nameμ μ΄κΈ°κ°μ μ§μ ν΄μ€μΌ ν¨
this.name = name;
}
hello(): void {
console.log(`μλ
νμΈμ! ${this.name} μ
λλ€.`);
}
};
const person = new Person('Mark');
person.hello(); // μλ
νμΈμ! Mark μ
λλ€.
π interface extends interface
interface λΌλ¦¬ μμ(extends)ν΄λ³΄μ.
interface Person {
name: string;
age?: number;
};
interface Korean extends Person {
city: string;
};
const k: Korean = {
name: 'μ΄μ
μ¬',
city: 'μμΈ'
};
π function interface
ν¨μμ νμ 체ν¬λ ν λΉν λκ° μλλΌ μ¬μ©ν λ νλ€λ μ μ λͺ μ¬νμ.
interface HelloPerson {
// (name: string, age: number): void;
(name: string, age?: number): void;
};
let helloPerson: HelloPerson = function (name: string) {
console.log(`μλ
νμΈμ! ${name} μ
λλ€.`);
};
helloPerson('Mark'); // μλ
νμΈμ! Mark μ
λλ€.
π Readonly Interface Properties
μΈν°νμ΄μ€ μμ±μ readonly (μ½κΈ° μ μ©)μΌλ‘ μ§μ ν΄λ³΄μ.
νλ‘νΌν°κ° ν λ² λ§λ€μ΄μ§κ³ λ°λμ§ μλ κ°μ΄λΌλ©΄ readonly λ₯Ό λΆμ¬μΌ νλ€.
interface Person8 {
name: string;
age?: number;
readonly gender: string;
};
const person: Person8 = {
name: "Mark",
gender: "male",
};
person.gender = "female" // μλ¬
π type alias vs interface
type alias μ interface λ₯Ό λΉκ΅ν΄λ³΄μ
function
// type alias
type EatType = (food: string) => void;
// interface
interface IEat {
(food: string): void;
}
array
// type alias
type PersonList = string[];
// interface
interface IPersonList {
[index: number]: string;
}
intersection
interface ErrorHandling {
success: boolean;
error?: { message: string };
}
interface ArtistsData {
artists: { name: string }[];
}
// type alias
type ArtistsResponseType = ArtistsData & ErrorHandling;
// interface
interface IArtistsResponse extends ArtistsData, ErrorHandling {} //λ€μ€ μμ
let art: ArtistsResponseType;
let iar: IArtistsResponse;
union types
interface Bird {
fly(): void;
layEggs(): void;
}
interface Fish {
swim(): void;
layEggs(): void;
}
// type alias
type PetType = Bird | Fish;
// interfaceλ λΆκ°
interface IPet extends PetType {} // error TS2312: An interface can only extend an
object type or intersection of object types with statically known members.
class Pet implements PetType {} // error TS2422: A class can only implement an
object type or intersection of object types with statically known members.
Declaration Merging
// type aliasλ λΆκ°
type MergingType = {
a: string;
};
type MergingType = { //μ΄λ¦μ΄ λκ°μμ μλ¬
b: string;
};
// interface
interface MergingInterface {
a: string;
}
interface MergingInterface {
b: string;
}
let mi: MergingInterface;
mi. // aμ bκ° λ¨Έμ§λμ΄ λ λ€ λμ¨λ€.
'TypeScript' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
π Classes (0) | 2022.12.19 |
---|---|
π TypeScript Compiler (0) | 2022.12.15 |
π Type System (0) | 2022.12.13 |