TypeScript νμ μ€ν¬λ¦½νΈ κΈ°μ΄ -2
μμνλ©΄μ
νμ μ€ν¬λ¦½νΈμ κΈ°μ΄λ₯Ό 곡λΆν©λλ€.
ν΄λμ€ μ μ
- ν΄λμ€λ implements λ₯Ό μ¬μ©ν©λλ€.
- ν΄λμ€λ κ°μ²΄μ λΉμ·ν΄λ³΄μ΄μ§λ§, μμμ ν μ μμ΅λλ€.
// TS
interface Car{
color:string;
wheels:number;
start():void;
} // μμ±μ(constructor) κΉμ§λ control ν μ μλ€
class Bmw implements Car{ // ':' λ§κ³ implements λ₯Ό μ¬μ©ν΄μΌνλ€
color; // TS μμ μλ΅νκ² λλ©΄ BMW μμ colorλ₯Ό μ°Ύμ§ λͺ»νλ€. λ³μλ₯Ό κΌ λ§λ€μ΄ μ£Όμ΄μΌνλ€.
wheels=4; // TS μμ μλ΅νκ² λλ©΄ wheelsλ₯Ό μ°Ύμ§ λͺ»νλ€. BMW class μ Wheels λ³μλ₯Ό λ§λ€μ΄ μ£Όμ΄μΌνλ€.
constructor(c:string){
this.color=c;
}
start():void{
console.log('go....!');
}
}
const car1 = new Bmw('green');
console.log(car1);
car1.start();
// JS
class Bmw {
constructor(c) { // constructor(μμ±μ ν¨μ)λ μ²μμ μλμΌλ‘ μμ (λ©€λ² λ³μλ₯Ό μ΄κΈ°νν΄μ€λ€)
this.wheels = 4; // λ©€λ² λ³μ
this.color = c;
}
start() {
console.log('go....!');
}
}
const car1 = new Bmw('green'); // car1 μ μΈμ€ν΄μ€
console.log(car1);
car1.start();
extends (νμ₯)
interface Car{ // λΆλͺ¨ μΈν°νμ΄μ€
color:string;
wheels:number;
start():void;
}
interface Car2 extends Car{ // μμ μΈν°νμ΄μ€
door:number;
stop(d:number):number;
}
// interface Car2{ // μμ μΈν°νμ΄μ€
// color:string;
// wheels:number;
// door:number;
// start():void;
// stop():void;
// }
class Bmw implements Car{
color;
wheels=4;
constructor(c:string){
this.color=c;
}
start():void{
console.log('Bmw go....!');
}
}
const car1 = new Bmw('green');
console.log(car1);
car1.start();
class Benz implements Car2{
color='red';
wheels=4;
door;
constructor(c:number){
this.door=c;
}
start():void{
console.log('Benz go....!');
}
stop(d:number):number{
console.log('Benz stop....!');
this.door = d;
return this.door;
}
}
const car2 = new Benz(4);
car2.wheels = 10;
car2.color = 'gray';
car2.door = 100;
console.log(car2);
car2.start();
let aaa:number = 0;
aaa = car2.stop(200);
console.log(aaa);
// κ²°κ³Ό
[LOG]: Bmw: {
"wheels": 4,
"color": "green"
}
[LOG]: "Bmw go....!"
[LOG]: Benz: {
"color": "gray",
"wheels": 10,
"door": 100
}
[LOG]: "Benz go....!"
[LOG]: "Benz stop....!"
[LOG]: 200
extends (νμ₯-2)
// interface Pro{
// color:string;
// wheels:number;
// name:string;
// price:number;
// start():void;
// open():void;
// buy():void;
// }
interface Car {
color:string;
wheels:number;
start():void;
}
interface Toy{
name:string;
open():void;
}
interface ToyCar extends Car, Toy {
price:number;
buy():void;
}
class Product implements ToyCar{
color='red';
wheels=4;
name='doll';
price=500;
start():void{
console.log('start....!');
}
open():void{
console.log('open stop....!');
}
buy():void{
console.log('buy stop....!');
}
}
const person1 = new Product();
console.log(person1);
person1.start();
person1.open();
person1.buy();
ν¨μ νμ μ μ λ°©λ²
μμ 1
function add(num1:number , num2:number):number{
return num1+num2;
}
console.log(add(10,20)); // 30
function isAdult(age:number):boolean{
return age > 19;
}
console.log(isAdult(30)); // true
μμ 2
- λ§€κ°λ³μλ₯Ό μ λ¬ν΄ μ£Όμ§ μλλ€λ©΄ TS λ μλ¬κ° λ°μν©λλ€.
function hello(name:string):string{
return `Hello, ${name || "world"}`; // nameμ΄ λ€μ΄μ€λ©΄ name μΆλ ₯, μλλΌλ©΄ "world" λ₯Ό μΆλ ₯νλΌ
}
const result = hello(); // μλ¬λ°μ
const result2 = hello('kim');
console.log(result, result2);
- μ΄λ₯Ό μ‘κΈ° μν΄μ μ νμ λ§€κ°λ³μ ? λ₯Ό μ¬μ©ν©λλ€.
// μλ¬κ° λμ€μ§ μκ² μμ±νλ €λ©΄? μ νμ λ§€κ°λ³μλ₯Ό μ΄μ©νμ!
function hello(name?:string){ // λ³μ? -> μ νμ λ§€κ°λ³μ
return `Hello, ${name || "world"}`;
}
const result = hello();
const result2 = hello('kim');
console.log(result, result2);
- μ νμ λ§€κ°λ³μλ₯Ό μ¬μ©νμ§ μκ³ ifλ¬Έμ μ¬μ©νλ€λ©΄?
function hello(name:string):string{
if(name == undefined) {
return `Hello, world`
}else{
return `Hello, ${name}`
}
}
- μ νμ λ§€κ°λ³μλ₯Ό μ¬μ©νμ§ μκ³ λ§€κ°λ³μμ μ΄κΈ° κ°μ μ§μ ν΄μ€μ μ¬μ©ν μλ μμ΅λλ€.
function hello2(name = 'world'):string{ // λ³μ? -> μ νμ λ§€κ°λ³μ
return `Hello, ${name}`;
}
const result3 = hello2();
const result4 = hello2('kim');
console.log(result3, result4);
λλ¨Έμ§ λ§€κ°λ³μ
function add(...nums:number[]):number {
return nums.reduce((result, num) => result + num);
}
console.log(add(1, 2, 3)); // 6
console.log(add(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); // 55
μμ 4
interface User {
name:string;
age:number;
}
function join(name:string, age:number):User {
return{
name,
age
};
};
const person1:User = join('μ νλ§', 60);
console.log(person1);
/*
{
"name": "μ νλ§",
"age": 60
}
*/
/*
// interface μμ΄ μ§μ λͺ
μν΄μ£Όλ λ°©λ²
function join(name:string, age:number):{name:string, age:number} {
return{
name,
age
};
};
const person1:{name:string, age:number} = join('μ νλ§', 60);
console.log(person1);
*/
// this? κ°μ²΄λ class μμ μ¬μ©
νμ΄νν¨μλ‘ μμ
interface User {
name:string;
age:number;
}
interface Func {
(name:string, age:number):User;
}
const join:Func = (name, age) => {
return {
name,
age
};
}
const person1:User = join('μ νλ§', 60);
console.log(person1);
리ν°λ΄(Literal types) / μ λμ¨(Union types) / κ΅μ°¨νμ (Intersection types)
리ν°λ΄
- 리ν°λ΄μ΄λ? typeμ μλ΅νλλΌλ, μ΄κΈ°κ°μ λ°λΌ typeμ΄ κ³ μ λλ€λ κ² μ¦, μ νν κ°μ΄ μ§μ λ¨μ λ§ν©λλ€.
- λ¬Έμμ΄μ΄λ μ«μμ μ νν κ°μ μ§μ νλ€λ κ²μ λλ€.
μ λμ¨ → | (λλ)
- μ¬λ¬κ°μ μ νμ§λ₯Ό μ€ μ μλ νμ μ λλ€.
const userName1 = 'Bob'; // type -> 'Bob' (μμ μ΄κΈ° λλ¬Έμ κ° μμ²΄κ° typeμ΄ λλ€)
let userName2 = 'Tom'; // type -> string
let userName3: string | number;
userName3 = 'μ λ¨μ';
userName3 = 100;
console.log(userName1, userName2, userName3);
type Job = "police" | "developer" | "teacher";
interface User{
name : string;
job : Job;
}
const user:User ={
name : "Bob",
job : "developer" // job : "student" -> μλ¬
}
console.log(user.name, user.job);
κ΅μ°¨νμ -> & (μ¬λ¬κ°μ νμ μ νλλ‘ ν©μΉλ€)
interface Car{
name : string;
start(): void;
}
interface Toy{
name : string;
color : string;
price: number;
}
const toyCar : Toy & Car ={
name: 'λ²μ€',
color: 'blue',
price: 10000,
start(){
console.log(this.name, this.color, this.price+'μ');
}
}
toyCar.start(); // "λ²μ€", "blue", "10000μ"
class ES6 (ν΄λμ€ λ³΅μ΅)
JS
- μλ°μ€ν¬λ¦½νΈ(.js) μμλ μ€λ₯κ° λ°μνμ§ μμ΅λλ€.
class Car{
constructor(color){
this.color = color;
}
start(){
console.log(this.color + " car start!");
}
}
const bmw = new Car("red");
bmw.start();
TS
- .tsμμλ color λΆλΆμμ μ€λ₯ 3κ°κ° λ°μν©λλ€.
- ν΄λμ€ λ§΄λ²λ³μλ₯Ό κΌ μ μΈμ ν΄μ£Όμ΄μΌ ν©λλ€.(.ts)
class Car{
color: string;
constructor(color: string){
this.color = color;
}
start(){
console.log(this.color + " car start!");
}
}
const bmw = new Car("red");
bmw.start();
public μ¬μ©
public μ΄λ? → μ μ μ΄λΌλ μλ―Έλ₯Ό κ°μ§κ³ μμ΅λλ€.
class Car{
// color: string;
constructor(public color: string){ // constructor λ΄λΆμμ public μ¬μ©
this.color = color;
}
start(){
console.log(this.color + " car start!");
}
}
const bmw = new Car("red");
bmw.start();
readonly μ¬μ©(μ½κΈ°μ μ©)
class Car{
// color: string;
constructor(readonly color: string){
this.color = color;
}
start(){
console.log(this.color + " car start!");
}
}
const bmw = new Car("red");
bmw.start();
// bmw.color='red'; // μλ¬ : μ½κΈ° μ μ©μ΄λΌ κ°μ λ°κΏ μ μλ€
- public / read-only λ νλΌλ―Έν°μ μμ(const)λ₯Ό λ§λ€μ΄ μ£Όλ κ²μ΄λΌκ³ μκ°νλ©΄ μ΄ν΄κ° μ½μ΅λλ€.
βββββ μ κ·Ό μ νμ(Access Modifier) βββββ
- public → μμ ν΄λμ€, ν΄λμ€ μΈμ€ν΄μ€ λͺ¨λ μ κ·Ό κ°λ₯ (μλ΅νλ©΄ κΈ°λ³Έκ°)
- private → ν΄λΉ ν΄λμ€ λ΄λΆμμλ§ μ κ·Ό κ°λ₯ (μμ ν΄λμ€ μ¬μ© λΆκ°)
- protected → μμν΄λμ€μμ μ κ·Ό κ°λ₯/ ν΄λμ€ μΈμ€ν΄μ€μμ μ κ·Ό λΆκ°
ν΄λμ€ μΈμ€ν΄μ€ → μ΄μ©κ³ = new ddd → μ΄μ©κ³ .name μ΄λ°μμ μ κ·Όμ΄ λΆκ°λ₯ν©λλ€.
public
class Car{
public name:string = "λ₯μ°¨";
color:string;
constructor(color:string){
this.color=color;
}
public start(){ // ν¨μμλ public μ¬μ©μ΄ κ°λ₯νλ€
console.log('λΆλͺ¨ν΄λμ€ strat!!',this.name, this.color);
}
}
class Bmw extends Car{
constructor(color:string){
super(color);
}
showName(){
console.log('μμν΄λμ€ strat!!', this.name , this.color); // λΆλͺ¨μκ²μ λ¬Όλ €λ°μ name, color (μμ ν΄λμ€μμλ μ¬μ©ν μ μλ€)
}
}
const mycar = new Bmw("red");
mycar.name="μμ°¨"; // μΈμ€ν΄μ€μμ μ§μ κ°μ λ°κΏ μ μλ€ (μΈμ€ν΄μ€μ μ κ·Ό κ°λ₯νλ€)
mycar.start(); // "λΆλͺ¨ν΄λμ€ strat!!", "μμ°¨", "red"
mycar.showName(); // "μμν΄λμ€ strat!!", "μμ°¨", "red"
private
class Car{
private name:string = "λ₯μ°¨";
color:string;
constructor(color:string){
this.color=color;
}
private start(){ // ν¨μμλ public μ¬μ©μ΄ κ°λ₯νλ€
console.log('λΆλͺ¨ν΄λμ€ strat!!',this.name, this.color);
}
}
class Bmw extends Car{
constructor(color:string){
super(color);
}
showName(){
console.log('μμν΄λμ€ strat!!', this.name , this.color); // λΆλͺ¨μκ²μ λ¬Όλ €λ°μ name, color (μμ ν΄λμ€μμλ μ¬μ©ν μ μλ€)
}
}
const car = new Car("pink");
car.color = "blue";
console.log(car.color);
const mycar = new Bmw("red");
mycar.name = "μμ°¨"; // μλ¬ -> μμ ν΄λμ€μμ μ κ·Όν μ μλ€.
mycar.color = "green"
mycar.start(); // μλ¬ -> μμ ν΄λμ€μμ μ κ·Όν μ μλ€.
mycar.showName(); // "μμν΄λμ€ strat!!", "μμ°¨", "red"
protected
class Car{
protected name:string = "λ₯μ°¨";
color:string;
constructor(color:string){
this.color=color;
}
protected start(){
console.log('λΆλͺ¨ν΄λμ€ strat!!', this.name, this.color);
}
}
class Bmw extends Car{
constructor(color:string){
super(color);
}
showName(){
console.log('μμν΄λμ€ strat!!', this.name , this.color); //μμ ν΄λμ€μμλ μ¬μ©ν μ μλ€
super.start();
}
}
const car = new Car("pink");
car.color = "blue";
const mycar = new Bmw("red");
// mycar.name="μμ°¨"; // μλ¬ : μΈμ€ν΄μ€μμ μ§μ κ°μ λ°κΏ μ μλ€
// mycar.start();
mycar.showName();
μ λ€λ¦
- μ λ€λ¦μ νΉμ νμ μ μ΄μ©ν΄μ ν νλ¦Ώμ²λΌ ν¨μλ₯Ό λ§λ€ μ μμ΅λλ€
ν¨μμ λ§€κ°λ³μμ μ λ¬ μΈμμ νμ μ΄ λ€λ₯Όλ…
function getSize(arr: number[]): number{
return arr.length;
}
const arr=[1,2,3];
console.log(getSize(arr)); //3
const arr2 = ['a','b','c','d','e'];
console.log(getSize(arr2)); //μλ¬κ° λ°μνλ€
- μ λμ¨ λ°©μμ μ¬μ©νλ λ°©λ²
function getSize(arr: number[] | string[]): number{
return arr.length;
}
const arr=[1,2,3];
console.log(getSize(arr)); // 3
const arr2 = ['a','b','c','d','e'];
console.log(getSize(arr2)); // 5
- μ λ€λ¦μ μ¬μ©νλ λ²
function ν¨μλͺ
<νμ
νλΌλ―Έν°>(λ§€κ°λ³μ:νμ
νλΌλ―Έν°):νμ
{}
function getSize<T>(arr: T[]):number{
return arr.length;
}
const arr = [1, 2, 3];
console.log(getSize<number>(arr));
const arr2 = ['a','b','c','d','e'];
console.log(getSize<string>(arr2));
const arr3 = [true,false,false,true];
console.log(getSize<Boolean>(arr3));
// any λ₯Ό μ°λ κ²½μ°λ?? type scriptλ₯Ό μΈ μ΄μ κ° μλ€. κ·Έλ₯ vanilaλ‘ μ°λ©΄ λλ€.
// <νμ
νλΌλ―Έν°> -> <T>,<A>,<X>... μ무 κ°μ΄λ μ¬μ©κ°λ₯ νλ€
μ λ€λ¦ μμ 2
interface Mobile<T>{
name: string;
price: number;
option: T;
}
/*
const obj1: Mobile<object> ={
name: 'μΌμ± s20',
price: 100,
option: {
color: 'red',
coupon: false
}
};
*/
const obj1: Mobile<{color:string; coupon:boolean}> ={
name: 'μΌμ± s20',
price: 100,
option: {
color: 'red',
coupon: false
}
};
console.log(obj1.name , obj1.price +'λ§μ', 'μμ:'+obj1.option.color, 'μΏ ν°:'+obj1.option.coupon);
const obj2: Mobile<string> ={
name: 'μΌμ± s22',
price: 120,
option: 'android OS'
};
console.log(obj2.name , obj2.price +'λ§μ', obj2.option);
λ§λ¬΄λ¦¬νλ©°
νμ μ€ν¬λ¦½νΈμ κΈ°μ΄λ₯Ό 1, 2νΈμ κ±Έμ³μ 곡λΆν΄ 보μμ΅λλ€.
λ³μ λΏλ§μλλΌ ν¨μκΉμ§ λͺ¨λ κ³³μ νμ μ μ§μ ν΄μ€μΌλ‘μ¨ μ€λ₯λ₯Ό μ΅μν νλ€λ κ²μ΄ λ§€λ ₯μ μΌλ‘ λ€κ°μμ΅λλ€. μλ°μ€ν¬λ¦½νΈ μΈμμμ κ°μ₯ λ§μ΄ κ²ͺλμΌ "μ€λ₯λ μλ¨λλ° κ°μ΄ μλμμ"κ° μλ νμ μ€ν¬λ¦½νΈ! νλ²μ―€μ κΌ κ³΅λΆν΄λ³΄μλ©΄ μ’μ λ― μΆμ΅λλ€.
λκΈκ³Ό 곡κ°μ νμ νμμ λλ€!