Using Interfaces to Describe Types in TypeScript

Share this video with your friends

Send Tweet

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.

Oktay Bilgin
Oktay Bilgin
~ 8 years ago

Great clean voice! Great Tutorial anyway! I WANT MORE!

Ari Picker
Ari Picker(instructor)
~ 8 years ago

Thanks Oktay! Glad you enjoyed the video. This totally made my day.

Sports Whispers
Sports Whispers
~ 7 years ago

I seem to be getting: Error:(..., ...) TS2683:'this' implicitly has type 'any' because it does not have a type annotation.. :/

Sports Whispers
Sports Whispers
~ 7 years ago

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. :/

Ari Picker
Ari Picker(instructor)
~ 7 years ago

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.

Cam Kida
Cam Kida
~ 7 years ago

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"

Amit Erandole
Amit Erandole
~ 7 years ago

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;
}
Daniel Ram
Daniel Ram
~ 7 years ago

this, I love.

Mike
Mike
~ 6 years ago

Ah.Mazing. A little fast but that just means i get to watch it over and over :) Thank you!

Guillermo
Guillermo
~ 6 years ago

Ari, this course is just amazing, Thank you so much!

Nick P
Nick P
~ 6 years ago

inebriationLevel

Etenne-Joseph Charles
Etenne-Joseph Charles
~ 4 years ago

After seeing it a second time, it all became clear, probably THE most important chapter of the course up till now - woaw🔥