0

How should I go about avoiding the use of "any" in my state interfaces, such as

export interface ITestState{
  listItems : any // this triggers a lint warning
}

when it's not exactly known what fields are defined for a list?

lint gives the warning

error @typescript-eslint/no-explicit-any: Unexpected any. Specify a different type.

Of course, nothing breaks when using "any", but I'm looking to understand what would be the best way to deal with this warning.

2 Answers 2

2

You can disable (and re-enable specific) eslint warnings at the line, block, file, an project levels. Here's how to disable the warning for the case you mentioned:

export interface ITestState{
  listItems : any // eslint-disable-line @typescript-eslint/no-explicit-any
}

For more information see: SharePoint Framework (SPFx) v1.15 and the attack of ESlint | Disabling ESLint rules

2
  • Thank you Rob. I know it can be disabled, but I would like to know how to define the listItems object without using 'any'. It can be defined as an object, but then you run in to errors elsewhere in your code like Property 'x' does not exist on type
    – JJD
    Commented Dec 11, 2022 at 10:11
  • I don't think there's an alternative to using any if you don't know what fields are going to be in the list. You could change the type of listItems to any[] so the code at least knows it's working with an array. Commented Dec 11, 2022 at 12:35
0

A bit late, but nonetheless:

  • maybe listItems: unknown[] will do the trick?

  • or a bit more specific: listItems: { [key: string]: unknown }[]?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.