Ayesh Weerasinghe
Ayesh Weerasinghe

Reputation: 742

Disable `noImplicitAny` TS rule for a single line

I have enabled "noImplicitAny": true in tsconfig.json and I'm getting TS7053: Element implicitly has an 'any' type because expression of type 'any' can't be used to index type '{}' error in my code space.

I want to disable this rule for a single line and I tried with below code, but it didn't work.

const mockElement = () => ({
  mount: jest.fn(),
  destroy: jest.fn(),
  on: jest.fn(),
  update: jest.fn(),
});

const mockElements = () => {
  const elements = {};
  return {
    create: jest.fn((type) => {
      // getting error from the bellow line
      // tslint:disable-next-line: no-implicit-any
      elements[type] = mockElement();
      // getting error from the bellow line
      // tslint:disable-next-line: no-implicit-any
      return elements[type];
    }),
    getElement: jest.fn((type) => {
      // getting error from the bellow line
      // tslint:disable-next-line: no-implicit-any
      return elements[type] || null;
    }),
  };
};

Any suggestions for disabling the rule ONLY for a single line? (setting "noImplicitAny": false in tsconfig.json would work but not acceptable as it disables the rule at all)

Upvotes: 3

Views: 7555

Answers (2)

ovirta
ovirta

Reputation: 41

You can use @ts-expect-error ts directive to ignore error eg:

// @ts-expect-error Parameter 'name' implicitly has an 'any' type.ts(7006)

More information: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-9.html#-ts-expect-error-comments

Upvotes: 2

dcsan
dcsan

Reputation: 12315

it's an ESLint thing, not a typescript compiler thing. try this in your source

// eslint-disable-next-line @typescript-eslint/no-explicit-any

tho, this is for EXplicit any, but I think you can workaround by just defining it explicitly on the one line you need?

Upvotes: -3

Related Questions