microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
101.21k stars 12.51k forks source link

Document indexOf return value when not found #60569

Open Pascal-So opened 1 day ago

Pascal-So commented 1 day ago

fixes #60570

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

Screenshot_20241123_133946

But for strings, this information was missing thus far:

Screenshot_20241123_134018

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.

I also noticed that the same information was missing for all the TypedArrays, so I went ahead and extended the docs there as well.

typescript-bot commented 1 day ago

This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise.

Pascal-So commented 1 day ago

@microsoft-github-policy-service agree