![[블로깅]타입스크립트 문법(2):enum, 인터페이스, 타입별칭, 타입추론, 클래스](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FWBejg%2FbtsIEnK2ZSa%2Fe2RUTq1JogKTujjPvlPiMk%2Fimg.jpg)
[블로깅]타입스크립트 문법(2):enum, 인터페이스, 타입별칭, 타입추론, 클래스BootCamp2023. 5. 31. 13:39
Table of Contents
enum - 열거형
enum Color {
Red,
Green,
Blue,
}
Interface - 객체 구조 정의
interface User {
name: string;
age: number;
}
타입 별칭
type MyString = string;
let str1: string = 'hello!';
// string 타입처럼 사용할 수 있습니다.
let str2: MyString = 'hello world!';
클래스 - ts
class Person {
name: string;
age: number;
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
greet(): void {
console.log(`안녕하세요, 제 이름은 ${this.name}이고, ${this.age}살 입니다.`);
}
}
실습코드
JavaScript를 TypeScript로 포팅 하기 - 인터페이스와 상속
/* 코드를 작성한 뒤
1. tsc src/index.ts
2. node src/index.js
순으로 터미널에 입력하여 결과를 확인해 주세요.
*/
/* 실습 1 */
interface User {
name: string;
age: number;
}
//Student 인터페이스를 작성합니다.
interface Student extends User {
grade: number;
}
//Student 인터페이스를 받는 kimcoding을 완성합니다.
//kimcoding의 이름은 김코딩이고, 나이는 20세이며, 학년은 1입니다.
const kimcoding: Student = {
name: 'kimcoding',
age: 20,
grade: 1,
};
console.log(kimcoding);
/* 코드를 작성한 뒤
1. tsc src/index.ts
2. node src/index.js
순으로 터미널에 입력하여 결과를 확인해 주세요.
*/
/* 실습 2 */
interface Color {
name: string;
brightness: number;
}
interface ClothesType {
kind: string;
length: number;
}
//Closet 인터페이스를 작성합니다.
interface Closet extends Color, ClothesType {
season: string;
}
//Closet 인터페이스를 받는 skirt를 완성합니다.
//skirt는 노란색이며, 밝기는 0이고, 종류는 치마이며 길이는 5 정도로 중간이며 여름용입니다.
const skirt: Closet = {
name: 'yellow',
brightness: 0,
kind: 'skirt',
length: 5,
season: 'summer',
};
console.log(skirt);
JavaScript를 TypeScript로 포팅 하기 - 타입 별칭
/* 코드를 작성한 뒤
1. tsc src/index.ts
2. node src/index.js
순으로 터미널에 입력하여 결과를 확인해 주세요.
*/
//Todo 타입을 정의합니다.
type Todo = {
id: number;
title: string;
isDone: boolean;
};
//Todo 타입을 사용해 객체를 정의합니다.
const show: Todo = {
id: 0,
title: 'what',
isDone: true,
};
//Todo 타입을 사용한 객체를 출력해봅니다.
function getTodo(todo: Todo): void {
console.log(todo);
}
getTodo(show);
JavaScript를 TypeScript로 포팅 하기 - 클래스
/* 코드를 작성한 뒤
1. tsc src/index.ts
2. node src/index.js
순으로 터미널에 입력하여 결과를 확인해 주세요.
*/
class Counter {
value: number;
constructor() {
this.value = 0;
}
increase() {
this.value++;
}
decrease() {
this.value--;
}
getValue() {
return this.value;
}
}
let counter1 = new Counter();
counter1.increase();
console.log(counter1.getValue());
/* 코드를 작성한 뒤
1. tsc src/index.ts
2. node src/index.js
순으로 터미널에 입력하여 결과를 확인해 주세요.
*/
class Animal {
name: string;
constructor(theName: string) {
this.name = theName;
}
speak(sound = '왕왕!') {
console.log(`${this.name}(은/는) ${sound}하고 웁니다.`);
}
}
class Mouse extends Animal {
constructor(name: string) {
super(name);
}
speak(sound = '찍찍') {
super.speak(sound);
}
}
class Cat extends Animal {
constructor(name: string) {
super(name);
}
speak(sound = '야옹') {
super.speak(sound);
}
}
let jerry:Mouse = new Mouse('제리');
let tom:Cat = new Cat('톰');
jerry.speak();
tom.speak('냥냥');
'BootCamp' 카테고리의 다른 글
react에서 proxy설정하기(http-proxy-middleware) (0) | 2023.06.07 |
---|---|
[블로깅]github actions 사용해보기 (0) | 2023.06.05 |
[블로깅]타입스크립트 문법(1):타입,함수,유니온,인터섹션 (1) | 2023.05.30 |
[블로깅] Cookie, Session, Token, OAuth2.0 (0) | 2023.05.04 |
[블로깅]웹 표준과 웹 접근성 (0) | 2023.04.27 |
@두루마기 :: 내가해냄
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!