Home > OS >  why I have to set a implicitly type although I have interface added?
why I have to set a implicitly type although I have interface added?

Time:02-17

I have added a interface, so why I have to set a type on my function for each parameter ? (id, name, year)

interface trackType {
    id: string,
    name: string,
    year: number
}

const tracks: Array<string> = [];

function addTrack<trackType>(id, name, year) {
    
}

CodePudding user response:

In this case you should pass to addTrack the whole object. Consider this example:

interface TrackType {
  id: string,
  name: string,
  year: number
}

const tracks: Array<string> = [];

function addTrack({ id, name, year }: TrackType) { 
  
}
addTrack({ id: 'a', name: 'John', year: 100 })

Playground

  • Related