TIL ( ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ - ts )

2024. 7. 1. 22:21ใ†TIL

๐Ÿ’ก ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ( feat. ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ )

 

- ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋Š” ํด๋ž˜์Šค์˜ ๊ธฐ๋Šฅ์„ ์ฆ๊ฐ•ํ•˜๋Š” ํ•จ์ˆ˜๋กœ ์—ฌ๋Ÿฌ ํ•จ์ˆ˜์—์„œ ๊ณตํ†ต์œผ๋กœ ์ˆ˜ํ–‰๋˜๋Š” ๋ถ€๋ถ„์„ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋กœ ๋งŒ๋“ค์–ด ์‚ฌ์šฉํ•œ๋‹ค.

- ์ถ”๊ฐ€์ ์ธ ๊ธฐ๋Šฅ์„ ์ฃผ์ž…ํ•˜๊ฑฐ๋‚˜, ๊ธฐ์กด ๊ธฐ๋Šฅ์„ ์ˆ˜์ •ํ•˜๊ฑฐ๋‚˜, ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋ฉฐ, ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋Š” ์ฃผ๋กœ ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ์„ ๋†’์ด๊ณ , ๋ฐ˜๋ณต์ ์ธ ์ž‘์—…์„ ์ค„์—ฌ์ฃผ๋ฉฐ, ์ฝ”๋“œ์˜ ์œ ์ง€๋ณด์ˆ˜์„ฑ์„ ํ–ฅ์ƒ์‹œํ‚ค๋Š” ๋ฐ ์œ ์šฉํ•˜๋‹ค

 

1. ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ๊ธฐ๋ณธ ๊ตฌ์กฐ

function ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ช…(target: any, propertyKey?: string | symbol, descriptor?: PropertyDescriptor) { }

 - target ์€ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๊ฐ€ ์ ์šฉ๋œ ์š”์†Œ๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค. ex) ํด๋ž˜์Šค, ๋ฉ”์„œ๋“œ, ํ”„๋กœํผํ‹ฐ ๋“ฑ

- propertyKey๋Š” ๋ฐ์ฝ”๋ ˆํ‹ฐ์–ด๊ฐ€ ์ ์šฉ๋œ ํ”„๋กœํผํ‹ฐ๋‚˜ ๋ฉ”์„œ๋“œ์˜ ์ด๋ฆ„์„ ๋œปํ•œ๋‹ค.

- descriptor๋Š” ๋ฉ”์„œ๋“œ๋‚˜ getter๋‚˜ setter์˜ ํ”„๋กœํผํ‹ฐ ๋””์Šคํฌ๋ฆฝํ„ฐ๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค.

 

2. ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ์œ ํ˜•

 

1) ํด๋ž˜์Šค ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ (Class Decorator)

 - ํด๋ž˜์Šค ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋Š” ํด๋ž˜์Šค ์ „์ฒด์— ์ ์šฉ๋˜๋ฉฐ, ํด๋ž˜์Šค๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ ํŠน์ • ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ ์‚ฌ์šฉํ•œ๋‹ค.

function logClass(constructor: Function) {
  console.log(`ํด๋ž˜์Šค๊ฐ€ ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค: ${constructor.name}`);
}
@logClass
class Person {
  constructor(public name: string) {}
}

- ๋กœ๊ทธ ํด๋ž˜์Šค ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๊ฐ€ ์‚ฌ๋žŒ person ํด๋ž˜์Šค์—  ์ ์šฉ๋˜์–ด person  ํด๋ž˜์Šค๊ฐ€ ์ƒ์„ฑ ๋ ๋•Œ๋งˆ๋‹ค ์ฝ˜์†”์— ๋กœ๊ทธ๋ฅผ ์ถœ๋ ฅ์‹œํ‚จ๋‹ค.

 

2) ๋ฉ”์„œ๋“œ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ (Method Decorator)

- ๋ฉ”์„œ๋“œ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋Š” ํด๋ž˜์Šค์˜ ํŠน์ • ๋ฉ”์„œ๋“œ์— ์ ์šฉ๋˜๋ฉฐ, ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ ํŠน์ • ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ์— ์‚ฌ์šฉํ•œ๋‹ค.

function ์‹œ๊ฐ„์ธก์ •(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
  const ์›๋ณธ๋ฉ”์„œ๋“œ = descriptor.value;
  descriptor.value = function(...args: any[]) {
    const ์‹œ์ž‘์‹œ๊ฐ„ = Date.now();
    const ๊ฒฐ๊ณผ = ์›๋ณธ๋ฉ”์„œ๋“œ.apply(this, args);
    const ์ข…๋ฃŒ์‹œ๊ฐ„ = Date.now();
    console.log(`${propertyKey} ๋ฉ”์„œ๋“œ ์‹คํ–‰ ์‹œ๊ฐ„: ${์ข…๋ฃŒ์‹œ๊ฐ„ - ์‹œ์ž‘์‹œ๊ฐ„}ms`);
    return ๊ฒฐ๊ณผ;
  };
  return descriptor;
}
class ๊ณ„์‚ฐ๊ธฐ {
  @์‹œ๊ฐ„์ธก์ •
  ๋”ํ•˜๋‹ค(a: number, b: number) {
    return a + b;
  }
}
const ๋‚ด๊ณ„์‚ฐ๊ธฐ = new ๊ณ„์‚ฐ๊ธฐ();
๋‚ด๊ณ„์‚ฐ๊ธฐ.๋”ํ•˜๋‹ค(5, 3);
// => ๋”ํ•˜๋‹ค ๋ฉ”์„œ๋“œ ์‹คํ–‰ ์‹œ๊ฐ„: Xms

- ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ๋งˆ๋‹ค ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๊ฐ€ ์‹œ๊ฐ„์„ ์ธก์ •ํ•œ๋‹ค.

 

3) ์ ‘๊ทผ์ž ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ (Accessor Decorator)

- ์ ‘๊ทผ์ž ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋Š” ํด๋ž˜์Šค์˜ ํŠน์ • ์ ‘๊ทผ์ž(ex. getter/setter )์— ์ ์šฉ๋œ๋‹ค.  ์ ‘๊ทผ์ž๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ ํŠน์ • ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ์— ์‚ฌ์šฉํ•œ๋‹ค.

function ๋กœ๊ทธ์ ‘๊ทผ์ž(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
  const ์›๋ณธGetter = descriptor.get;

  descriptor.get = function() {
    console.log(`์ ‘๊ทผ์ž๊ฐ€ ํ˜ธ์ถœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค: ${propertyKey}`);
    return ์›๋ณธGetter?.apply(this);
  };
  return descriptor;
}
class person {
  private _age: number;
  constructor(age: number) {
    this._age = age;
  }
  @๋กœ๊ทธ์ ‘๊ทผ์ž
  get age() {
    return this._age;
  }
}
const john = new person(30);
console.log(john.age);

 

4) ํ”„๋กœํผํ‹ฐ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ (Property Decorator)

- ํ”„๋กœํผํ‹ฐ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋Š” ํด๋ž˜์Šค์˜ ํŠน์ • ํ”„๋กœํผํ‹ฐ์— ์ ์šฉ๋˜๋ฉฐ, ํ”„๋กœํผํ‹ฐ๊ฐ€ ์„ ์–ธ๋  ๋•Œ ํŠน์ • ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ์— ์‚ฌ์šฉํ•œ๋‹ค.

function logProperty(target: any, propertyKey: string) {
  console.log(`ํ”„๋กœํผํ‹ฐ๊ฐ€ ์„ ์–ธ๋˜์—ˆ์Šต๋‹ˆ๋‹ค: ${propertyKey}`);
}
class Book {
  @logProperty
  title: string;
  constructor(title: string) {
    this.title = title;
  }
}
const myBook = new Book('ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์™„๋ฒฝ ๊ฐ€์ด๋“œ'); // =>ํ”„๋กœํผํ‹ฐ๊ฐ€ ์„ ์–ธ๋˜์—ˆ์Šต๋‹ˆ๋‹ค: title

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

'TIL' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

TIL (DTO)  (0) 2024.07.03
TIL( TypeScript - Utility type )  (0) 2024.07.02
TIL ( prisma ์—†์ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค(MySQL) ์—ฐ๊ฒฐํ•˜๊ธฐ )  (0) 2024.06.28
TIL( TypeScriptํ•™์Šต )  (0) 2024.06.27
TIL ( TypeScript ํ•™์Šต)  (0) 2024.06.26