250x250
Rainbow๐ŸŒˆCoder
My dev Note๐Ÿ“’
Rainbow๐ŸŒˆCoder
์ „์ฒด ๋ฐฉ๋ฌธ์ž
์˜ค๋Š˜
์–ด์ œ
  • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (411)
    • ๊ณต์ง€์‚ฌํ•ญ (0)
    • Debugger (10)
      • Visual Studio Debugger (1)
      • Chrome DevTools (3)
      • Visual Studio Code Debugger (4)
      • eclipse (1)
      • intelliJ (1)
    • OOP (2)
      • OOP (2)
    • TypeScript (54)
      • ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ TypeScript (54)
    • Javascript (87)
      • Javascript (45)
      • Node.js (19)
      • React (5)
      • FE ๊ฐœ๋ฐœํ™˜๊ฒฝ์„ค์ • (3)
      • React์™€ Node ๊ฐ™์ด ๋•Œ๋ ค์žก๊ธฐ (6)
      • next.js (2)
      • pixi.js (7)
    • ๋งˆํฌ์—… (23)
      • Html & Css (23)
    • C# (80)
      • C# (12)
      • ์ด๊ฒƒ์ด C#์ด๋‹ค (68)
    • C++ (30)
      • c++ (27)
      • win api (3)
    • Unity (18)
      • Unity(๊ธฐ์ดˆ) (8)
      • Unity(C#์ค‘๊ธ‰) (5)
      • ์œ ๋‹ˆํ‹ฐ ํฌํ†ค(๋„คํŠธ์›Œํฌ) (4)
      • unity c# MyCode (1)
    • Java & Spring (29)
      • Java (11)
      • ์Šคํ”„๋ง (8)
      • Java Algorithm (9)
      • Javs Data Structures (1)
    • ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ (15)
      • ์ž๋ฃŒ๊ตฌ์กฐ (5)
      • ์•Œ๊ณ ๋ฆฌ์ฆ˜ (10)
    • ํ˜•์ƒ๊ด€๋ฆฌ (15)
      • Git (11)
      • ์†Œ์ŠคํŠธ๋ฆฌ (3)
    • ๊ทธ๋ž˜ํ”ฝ์Šค (7)
      • WebGl (7)
    • AWS (3)
      • aws (3)
    • ๋ฆฌ๋ˆ…์Šค (5)
      • ๋ฆฌ๋ˆ…์Šค (5)
    • ์ฑ… ๋ฆฌ๋ทฐ (13)
      • ํด๋ฆฐ์ฝ”๋“œ(์ฑ…๋ฆฌ๋ทฐ) (3)
      • ์œ ์ง€๋ณด์ˆ˜๊ฐ€๋Šฅํ•œ์ฝ”๋”ฉ์˜๊ธฐ์ˆ C#ํŽธ(์ฑ…๋ฆฌ๋ทฐ) (1)
      • ๋ฆฌํŒฉํ† ๋ง(์ž๋ฐ”์Šคํฌ๋ฆฝํŠธํŒ) (9)
    • Server (2)
      • ๊ฒŒ์ž„ ์„œ๋ฒ„(๋„คํŠธ์›Œํฌ, ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ,OS) (2)
    • ์„ค๊ณ„, ์•„ํ‚คํ…์ณ (4)
    • ํŒŒ์ด์ฌ (5)
    • ๋””์ž์ธํŒจํ„ด (2)
    • mocha (2)
    • Jest (1)
    • Spine (1)
    • ์ธ๊ณต์ง€๋Šฅ (1)
      • ํ˜ผ์ž๊ณต๋ถ€ํ•˜๋Š”๋จธ์‹ ๋Ÿฌ๋‹+๋”ฅ๋Ÿฌ๋‹ (1)

๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

  • ํ™ˆ
  • ํƒœ๊ทธ
  • ๋ฐฉ๋ช…๋ก

๊ณต์ง€์‚ฌํ•ญ

์ธ๊ธฐ ๊ธ€

ํƒœ๊ทธ

  • MySQL
  • ใ…ฃใ„ท
  • ์œ„์ž„
  • ์ปดํฌ์ง€์…˜

์ตœ๊ทผ ๋Œ“๊ธ€

์ตœ๊ทผ ๊ธ€

ํ‹ฐ์Šคํ† ๋ฆฌ

hELLO ยท Designed By ์ •์ƒ์šฐ.
Rainbow๐ŸŒˆCoder

My dev Note๐Ÿ“’

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

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

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

'TypeScript > ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ TypeScript' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] ์ปคํ”ผ๋จธ์‹  ํด๋ž˜์Šค๋งŒ๋“ค๊ธฐ ์‹ค์Šต  (0) 2022.04.19
[ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ์ง€์›, ๋ช…์‹œ  (0) 2022.04.19
[ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] class์˜ getter์™€ setter  (0) 2022.04.18
[ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] ๊ฐ์ฒด์ง€ํ–ฅ์  ์ปคํ”ผ๋จธ์‹  ๋งŒ๋“ค๊ธฐ : encapsulation  (0) 2022.04.18
[ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] ์ ˆ์ฐจ์ง€ํ–ฅ์  ์ปคํ”ผ๋จธ์‹  vs ๊ฐ์ฒด์ง€ํ–ฅ์  ์ปคํ”ผ๋จธ์‹   (0) 2022.04.18
    'TypeScript/ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ TypeScript' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
    • [ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] ์ปคํ”ผ๋จธ์‹  ํด๋ž˜์Šค๋งŒ๋“ค๊ธฐ ์‹ค์Šต
    • [ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ์ง€์›, ๋ช…์‹œ
    • [ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] class์˜ getter์™€ setter
    • [ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] ๊ฐ์ฒด์ง€ํ–ฅ์  ์ปคํ”ผ๋จธ์‹  ๋งŒ๋“ค๊ธฐ : encapsulation
    Rainbow๐ŸŒˆCoder
    Rainbow๐ŸŒˆCoder
    ๋ชฐ๋ผ๋„ ๊ฒฐ๊ตญ์€ ์•„๋Š” ๊ฐœ๋ฐœ์ž, ๊ทธ๋Ÿฐ ์‚ฌ๋žŒ์ด ๋˜๊ธฐ ์œ„ํ•œ ๋งค์ผ์˜ ํ•œ๊ฑธ์Œ

    ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”