matrix-js-sdk icon indicating copy to clipboard operation
matrix-js-sdk copied to clipboard

Enable `prefer-ts-expect-error`

Open ShadowJonathan opened this issue 4 years ago • 2 comments

https://typescript-eslint.io/rules/prefer-ts-expect-error/

This encourages to replace @ts-ignore with an expectation of an exact error, making the line error if it doesn't actually contain an error.

ShadowJonathan avatar Jan 23 '22 15:01 ShadowJonathan

Why?

turt2live avatar Jan 24 '22 18:01 turt2live

Consider the following snippet;

function isOptionEnabled(key: string): boolean {
  // @ts-expect-error: if key isn't in globalOptions it'll be undefined which is false
  return !!globalOptions[key];
};

globalOptions is not typed here, so it'll be an any type, making TS abandon nearly all type-checking on it.

However, together with another linter, such as no-unsafe-member-access, which prohibits all member access (.a, ["a"]) to anything typed any, this would allow the programmer to override this check, and assure that an error "is expected", and everything is fine.

However, the difference with @ts-ignore is that this'll instead raise an error if there are none to raise, in cases such as where globalOptions suddenly is typed (Record<string, any>), and the above line wouldn't create an error anymore. This'll detect false-positive assertions of error surpression, drawing the programmer's attention to a part of the program where once an error was expected, but now isn't anymore.

This'd help prevent subtle bugs by making sure the program stays invariant with expectations, this comment would narrow an expectation to "yeah, there's an error here".

ShadowJonathan avatar Jan 24 '22 21:01 ShadowJonathan