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
์ฑ๊ณต์ ์ผ๋ก ๊ณ ์ณ์ง๊ณ ์ ์๋ํ๋ ๋ชจ์ต ^^
'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 |