πŸ’‘ μΈν„°νŽ˜μ΄μŠ€(Interface)λž€?

Jake_YoungΒ·2021λ…„ 3μ›” 17일
0
post-thumbnail

μš°μ„  이 κΈ€μ˜ μ„€λͺ…κ³Ό μ˜ˆμ œκ°€ 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']
}
profile
μžλ°”μŠ€ν¬λ¦½νŠΈμ™€ 파이썬 그리고 컴퓨터와 λ„€νŠΈμ›Œν¬

0개의 λŒ“κΈ€