Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow ts-expect-error to specify the expected error code(s) #58021

Closed
6 tasks done
RamonBalthazar opened this issue Apr 1, 2024 · 2 comments
Closed
6 tasks done

Allow ts-expect-error to specify the expected error code(s) #58021

RamonBalthazar opened this issue Apr 1, 2024 · 2 comments

Comments

@RamonBalthazar
Copy link

RamonBalthazar commented Apr 1, 2024

πŸ” Search Terms

"ts-expect-error"

βœ… Viability Checklist

⭐ Suggestion

Allow the @ts-expect-error annotation to be narrowed to one or multiple error codes.

Example:

function getFirst(chars: string[]): string {
  // @ts-expect-error(2532)
  return chars[0].tolowercase()
}

For the above code, TS would:

  • Show an error for the misspelled toLowerCase (TS2551)
  • Ignore chars[0] being possibly undefined (TS2532)

You could specify one or multiple error codes in the brackets, comma separated.

πŸ“ƒ Motivating Example

When bringing code from other codebases with less strict rules, you sometimes would want to fix the typing issues over time. You could mark all of the failing lines with @ts-expect-error, to be fixed in subsequent code changes, but what if in the meantime someone makes an unrelated type mistake in one of those lines and it causes a run-time error?

Having the ability to specify what error you're expecting in those lines adds a layer of safety for a code you didn't really want to make unsafe.

πŸ’» Use Cases

1. What do you want to use this for?
To be more granular about what errors to expect.

2. What shortcomings exist with current approaches?
They allow for any error to exist in a given line.

3. What workarounds are you using in the meantime?
None

@RamonBalthazar RamonBalthazar changed the title Allow ts-expect-error to specify hexpected error code(s) Allow ts-expect-error to specify the expected error code(s) Apr 1, 2024
@MartinJohns
Copy link
Contributor

Duplicate of #45937. Used search terms: ts-expect-error in:title

@RamonBalthazar
Copy link
Author

Ah shoot, I missed that one πŸ€”
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants