It’s easy to pass the wrong value to a function. Typescript interfaces are great because they catch errors at compile time or in an IDE. In this lesson we’ll learn how to describe a type shape with Typescript interfaces.
Great clean voice! Great Tutorial anyway! I WANT MORE!
Thanks Oktay! Glad you enjoyed the video. This totally made my day.
I seem to be getting: Error:(..., ...) TS2683:'this' implicitly has type 'any' because it does not have a type annotation.
. :/
Interesting, so if I do:
let attackFunc = function(this: any, opponent: any, attackWith: any) {
it works! But if I try to do it in ES syntax:
let attackFunc = (this: any, opponent: any, attackWith: any) => {
it doesn't, still throws the same error. :/
I think you figured it out, but if you're still wondering this
isn't allowed as an arrow function argument.
Here's a reference to the issue on Github.
Interesting, so if I do:
let attackFunc = function(this: any, opponent: any, attackWith: any) {
it works! But if I try to do it in ES syntax:
let attackFunc = (this: any, opponent: any, attackWith: any) => {
it doesn't, still throws the same error. :/
that's because arrow functions doesn't bind their own "this"
I could add better typing to the attack function by making these changes:
interface AttackFunction {
(opponent: ComicBookCharacter, attackWith: number): number;
}
and then
function attack(
this: ComicBookCharacter,
opponent: ComicBookCharacter,
attackWith: number
): number {
opponent.health -= attackWith;
console.log(
`${this.alias} has just attacked ${opponent.alias}, who's health = ${
opponent.health
} `
);
return opponent.health;
}
this, I love.
Ah.Mazing. A little fast but that just means i get to watch it over and over :) Thank you!
Ari, this course is just amazing, Thank you so much!
inebriationLevel
After seeing it a second time, it all became clear, probably THE most important chapter of the course up till now - woaw🔥