TypeScript/ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ TypeScript

[ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] this ๊ด€๋ จ ์˜ค๋ฅ˜

Rainbow๐ŸŒˆCoder 2022. 4. 19. 11:32
728x90
function fancyDate() {
  return console.log(
    `${this.getDate()} / ${this.getMonth()} / ${this.getFullYear()}`
  );
}
fancyDate.call(new Date());

 

<์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€>

 

 

src/index.ts:74:8 - error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.

74     `${this.getDate()} / ${this.getMonth()} / ${this.getFullYear()}`
          ~~~~

src/index.ts:74:28 - error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.

74     `${this.getDate()} / ${this.getMonth()} / ${this.getFullYear()}`
                              ~~~~

src/index.ts:74:49 - error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.

74     `${this.getDate()} / ${this.getMonth()} / ${this.getFullYear()}`


Found 3 errors in the same file, starting at: src/index.ts:74

 

 

ํ•ด๋‹น

TypeScript์—์„œ 'this'๊ฐ€ ์•”์‹œ์ ์œผ๋กœ 'any' ์œ ํ˜• ์˜ค๋ฅ˜๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค  : ํ•ด๋‹น ์˜ค๋ฅ˜๋ฅผ ์ดํ•ดํ•˜๊ณ  ์ ์ ˆํ•˜๊ฒŒ ์ฝ”๋“œ๋ฅผ ๊ณ ์น˜๊ธฐ ์œ„ํ•ด ์•„๋ž˜ ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŒ…์„ ์ฐธ๊ณ  ํ•œ๋‹ค.

https://bobbyhadz.com/blog/typescript-this-implicitly-has-type-any

 

Fix - 'this' implicitly has type 'any' error in TypeScript | bobbyhadz

The error "this implicitly has type any" occurs when we use the `this` keyword outside of classes and its type cannot be inferred. To solve the error, add a type for the `this` keyword as the first parameter in the function.

bobbyhadz.com

"this implicitly have type any" ์˜ค๋ฅ˜๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ TypeScript๊ฐ€ ํ‚ค์›Œ๋“œ์˜ ์œ ํ˜•์„ ๊ฒฐ์ •ํ•  ์ˆ˜ ์—†์„ ๋•Œ ๋ฐœ์ƒํ•œ๋‹ค.

ํด๋ž˜์Šค ์™ธ๋ถ€ ๋˜๋Š” ์œ ํ˜•์„ ์œ ์ถ”ํ•  ์ˆ˜ ์—†๋Š” thisํ•จ์ˆ˜์—์„œ ํ‚ค์›Œ๋“œ ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋„ ๋ฐœ์ƒํ•œ๋‹ค. ํด๋ž˜์Šค ์™ธ๋ถ€์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๊ฒฝ์šฐ ๊ธฐ๋ณธ์ ์œผ๋กœ this์œ ํ˜•์ด any์ด๋‹ค. this์˜ค๋ฅ˜๋ฅผ ํ•ด๊ฒฐํ•˜๋ ค๋ฉด thisํ‚ค์›Œ๋“œ์˜ ์œ ํ˜•์„ ํ•จ์ˆ˜์˜ ์ฒซ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ถ”๊ฐ€ํ•œ๋‹ค.

 

class Employee {
  constructor(public name: string, public salary: number) {
    this.name = name;
    this.salary = salary;
  }
}

interface Employee {
  getSalary(): number;
}

//                        ๐Ÿ‘‡๏ธ add a parameter to type `this`
Employee.prototype.getSalary = function (this: Employee) {
  return this.salary;
};

const e1 = new Employee('Tom', 100);

console.log(e1.getSalary());

 

 

๋‹ค์Œ์€ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๋ฐฉ๋ฒ•์˜ ์˜ˆ

 

class Employee {
  first: string;
  last: string;

  constructor(first: string, last: string) {
    this.first = first;
    this.last = last;
  }

  getFullNameFunction() {
    return function () {
      // โ›”๏ธ Error 'this' implicitly has type
      // 'any' because it does not have a type annotation.
      return this.first + ' ' + this.last;
    };
  }
}

this์ค‘์ฒฉ๋œ ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ ์ปจํ…์ŠคํŠธ๋Š” ์ธ์Šคํ„ด์Šค getFullNameFunction๊ฐ€ ์•„๋‹ˆ๋‹ค.

Employee this

์ด ์ƒํ™ฉ์—์„œ ์˜ค๋ฅ˜๋ฅผ ํ•ด๊ฒฐํ•˜๋ ค๋ฉด ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๊ฐ€ ๋‘˜๋Ÿฌ์‹ธ๋Š” ๋ฒ”์œ„๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ค‘์ฒฉ ํ•จ์ˆ˜๋ฅผ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋กœ ๋ณ€ํ™˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค .

class Employee {
  first: string;
  last: string;

  constructor(first: string, last: string) {
    this.first = first;
    this.last = last;
  }

  getFullNameFunction() {
    return () => {
      console.log(this);
      // โœ… this is now instance of Employee
      return this.first + " " + this.last;
    };
  }
}

const e1 = new Employee("John", "Doe");

const func = e1.getFullNameFunction();

console.log(func()); // ๐Ÿ‘‰๏ธ "John Doe"

 

๊ทธ๋Ÿผ ์ฒ˜์Œ์œผ๋กœ ๋‹ค์‹œ ๋Œ์•„๊ฐ€์„œ

function fancyDate() {
  return console.log(
    `${this.getDate()} / ${this.getMonth()} / ${this.getFullYear()}`
  );
}
fancyDate.call(new Date());

์œ„์˜ ํ•จ์ˆ˜๋ฅผ ๊ณ ์ณ๋ณธ๋‹ค.

 

 

- TypeScript์—์„œ 'this'๊ฐ€ ์•”์‹œ์ ์œผ๋กœ 'any' ์œ ํ˜•์œผ๋กœ ์ถ”๋ก 

- ์œ ํ˜•์„ ์œ ์ถ”ํ•  ์ˆ˜ ์—†๋Š” thisํ•จ์ˆ˜์—์„œ ํ‚ค์›Œ๋“œ ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋„ ๋ฐœ์ƒ

- this์˜ค๋ฅ˜๋ฅผ ํ•ด๊ฒฐํ•˜๋ ค๋ฉด thisํ‚ค์›Œ๋“œ์˜ ์œ ํ˜•์„ ํ•จ์ˆ˜์˜ ์ฒซ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ถ”๊ฐ€ํ•œ๋‹ค.

 

function fancyDate(this: Date) {
  return console.log(
    `${this.getDate()} / ${this.getMonth()} / ${this.getFullYear()}`
  );
}
fancyDate.call(new Date()); //19 / 3 / 2022

์„ฑ๊ณต์ ์œผ๋กœ ๊ณ ์ณ์ง€๊ณ  ์ž˜ ์ž‘๋™ํ•˜๋Š” ๋ชจ์Šต ^^

 

 

 

 

 

 

 

 

728x90