μ°μ μ΄ κΈμ μ€λͺ κ³Ό μμ κ°
Ung-mo Lee
λμ λΈλ‘κ·Έμμ μμμ 미리 λ§μλ립λλ€.
https://poiemaweb.com/typescript-interface
interface
λ λͺ
λ Ήμ΄(?)λ₯Ό μμ£Όλ³΄κ² λμλ€.λ³μ
λ ν¨μ
, κ·Έλ¦¬κ³ ν΄λμ€
κ° λ§μ‘±ν΄μΌνλ μ΅μ κ·κ²©μ μ§μ ν μ μκ² ν΄μ£Όλ λꡬμ΄λ€.μ¬μ©μ μ μ μλ£ν
μΌλ‘λ μ¬μ©ν μ μλ€.
// μΈν°νμ΄μ€μ μ μ
interface Todo {
id: number;
content: string;
completed: boolean;
}
// λ³μ todoμ νμ
μΌλ‘ Todo μΈν°νμ΄μ€λ₯Ό μ μΈνμλ€.
let todo: Todo;
// λ³μ todoλ Todo μΈν°νμ΄μ€λ₯Ό μ€μνμ¬μΌ νλ€.
todo = { id: 1, content: 'typescript', completed: false };
// μΈν°νμ΄μ€μ μ μ
interface Todo {
id: number;
content: string;
completed: boolean;
}
let todos: Todo[] = [];
// νλΌλ―Έν° todoμ νμ
μΌλ‘ Todo μΈν°νμ΄μ€λ₯Ό μ μΈνμλ€.
function addTodo(todo: Todo) {
todos = [...todos, todo];
}
// νλΌλ―Έν° todoλ Todo μΈν°νμ΄μ€λ₯Ό μ€μνμ¬μΌ νλ€.
const newTodo: Todo = { id: 1, content: 'typescript', completed: false };
addTodo(newTodo);
console.log(todos)
// [ { id: 1, content: 'typescript', completed: false } ]
// ν¨μ μΈν°νμ΄μ€μ μ μ
interface SquareFunc {
(num: number): number;
}
// ν¨μ μΈν
νμ΄μ€λ₯Ό ꡬννλ ν¨μλ μΈν°νμ΄μ€λ₯Ό μ€μνμ¬μΌ νλ€.
const squareFunc: SquareFunc = function (num: number) {
return num * num;
}
console.log(squareFunc(10)); // 100
// μΈν°νμ΄μ€μ μ μ
interface ITodo {
id: number;
content: string;
completed: boolean;
}
// Todo ν΄λμ€λ ITodo μΈν°νμ΄μ€λ₯Ό ꡬννμ¬μΌ νλ€.
class Todo implements ITodo {
constructor (
public id: number,
public content: string,
public completed: boolean
) { }
}
const todo = new Todo(1, 'Typescript', false);
console.log(todo);
// μΈν°νμ΄μ€μ μ μ
interface IPerson {
name: string;
sayHello(): void;
}
/*
μΈν°νμ΄μ€λ₯Ό ꡬννλ ν΄λμ€λ μΈν°νμ΄μ€μμ μ μν νλ‘νΌν°μ μΆμ λ©μλλ₯Ό λ°λμ ꡬννμ¬μΌ νλ€.
*/
class Person implements IPerson {
// μΈν°νμ΄μ€μμ μ μν νλ‘νΌν°μ ꡬν
constructor(public name: string) {}
// μΈν°νμ΄μ€μμ μ μν μΆμ λ©μλμ ꡬν
sayHello() {
console.log(`Hello ${this.name}`);
}
}
function greeter(person: IPerson): void {
person.sayHello();
}
const me = new Person('Lee');
greeter(me); // Hello Lee
interface IDuck { // 1
quack(): void;
}
class MallardDuck implements IDuck { // 3
quack() {
console.log('Quack!');
}
}
class RedheadDuck { // 4
quack() {
console.log('q~uack!');
}
}
function makeNoise(duck: IDuck): void { // 2
duck.quack();
}
makeNoise(new MallardDuck()); // Quack!
makeNoise(new RedheadDuck()); // q~uack! // 5
interface IPerson {
name: string;
}
function sayHello(person: IPerson): void {
console.log(`Hello ${person.name}`);
}
const me = { name: 'Lee', age: 18 };
sayHello(me); // Hello Lee
function sayHello(person) {
console.log("Hello " + person.name);
}
var me = { name: 'Lee', age: 18 };
sayHello(me); // Hello Lee
interface UserInfo {
username: string;
password: string;
age? : number;
address?: string;
}
const userInfo: UserInfo = {
username: 'ungmo2@gmail.com',
password: '123456'
}
console.log(userInfo);
interface Person {
name: string;
age?: number;
}
interface Student extends Person {
grade: number;
}
const student: Student = {
name: 'Lee',
age: 20,
grade: 3
}
interface Person {
name: string;
age?: number;
}
interface Developer {
skills: string[];
}
interface WebDeveloper extends Person, Developer {}
const webDeveloper: WebDeveloper = {
name: 'Lee',
age: 20,
skills: ['HTML', 'CSS', 'JavaScript']
}
class Person {
constructor(public name: string, public age: number) {}
}
interface Developer extends Person {
skills: string[];
}
const developer: Developer = {
name: 'Lee',
age: 20,
skills: ['HTML', 'CSS', 'JavaScript']
}