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

Extend documentation of String.indexOf to mention what happens when substring is not found #60570

Open
6 tasks done
Pascal-So opened this issue Nov 23, 2024 · 0 comments Β· May be fixed by #60569
Open
6 tasks done

Extend documentation of String.indexOf to mention what happens when substring is not found #60570

Pascal-So opened this issue Nov 23, 2024 · 0 comments Β· May be fixed by #60569
Labels
Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript
Milestone

Comments

@Pascal-So
Copy link

πŸ” Search Terms

function documentation
jsdoc
NOT-FOUND

βœ… Viability Checklist

⭐ Suggestion

The documentation of String.indexOf should mention that -1 is returned when the substring is not found.

πŸ“ƒ Motivating Example

The documentation for indexOf on arrays already mentioned that the method returns -1 when the value is not found:

Image

But for strings, this information was missing thus far:

Image

Of course the user would have been able to puzzle this together by seeing that the return type is just number, not something like number | undefined, thus allowing them to figure out that the sentinel value probably must be -1. Nevertheless, having to stop and think about this is unnecessary friction for people who don't remember this fact by heart, for example people who jump between programming languages a lot.

πŸ’» Use Cases

  1. What do you want to use this for?

To get more useful information from the language server while writing TS, which reduces interruptions / context switching, allowing the user to be more productive.

  1. What shortcomings exist with current approaches?

The user has to stop and think about the not found case. Users coming from other languages might expect the indexOf function to maybe return null or undefined or throw an exception when the substring is not found.

  1. What workarounds are you using in the meantime?

In cases where I've personally been faced with this issue in the past, I quickly used a js console to check the behaviour.

@Pascal-So Pascal-So linked a pull request Nov 23, 2024 that will close this issue
@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Experience Enhancement Noncontroversial enhancements labels Nov 27, 2024
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Nov 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants