Home > Enterprise >  Element implicitly has an 'any' type because expression of type 'number' can
Element implicitly has an 'any' type because expression of type 'number' can

Time:05-20

Hi Can I get help how to fix this error: "Element implicitly has an 'any' type because expression of type 'number' can't be used to index type 'Number' No index signature with a parameter of type 'number' was found on type 'Number'". Its pagination task. How to fix this error ?

 <button> 
 disabled={currentPage == pages[pages.length - 1] ? true : false}
 </button>

 <button> 
 disabled={currentPage == pages[0] ? true : false}
 </button>

CodePudding user response:

It seems that the type of pages is any because it's not defined. Try defining the type of pages like pages: any[] or another array type.

CodePudding user response:

For anyone in the future. Typescript stopped complaining when I did this :

      const pages:any[number]= Math.ceil(totalItems / pageSize); 

CodePudding user response:

If pages is the total number of pages, so it should be of type number, not an array. I think that would make it easier to read your code.

const pages: number = Math.ceil(totalItems / pageSize);
<button disabled={currentPage === pages ? true : false}> 
 
 </button>

So if the currentPage is equal to pages, it means it's the last page, so disabled is true, else, it's false. Notice also how I included the disabled={...} inside the button tag (<button disabled={...} >), not inside of it. Not sure if it was a typo from your side, but anyway.

  • Related