Skip to content

String.startsWith, String.endsWith, and String.includes should have type assertion for when search string is a string subtype #57929

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

Closed
6 tasks done
KSXGitHub opened this issue Mar 25, 2024 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@KSXGitHub
Copy link
Contributor

πŸ” Search Terms

"startsWith type assertion"
"endsWith type assertion"
"includes type assertion"

βœ… Viability Checklist

⭐ Suggestion

When I do if (string.startsWith('https://')), I wish string turns into ${'https://'}${string} because I use string subtype heavily in my code.

I wish for the following:

interface String {
  startsWith<SearchString>(searchString: SearchString, position?: 0): this is `${SearchString}${string}`
  endsWith<SearchString>(searchString: SearchString, position?: undefined): this is `${string}${SearchString}`
  includes<SearchString>(searchString: SearchString, position?: 0): this is `${string}${SearchString}${string}`
}

πŸ“ƒ Motivating Example

declare function handleHttpsUrl(url: `https://${string}`): Promise<void>
declare const url: string

if (url.startsWith('https://') {
  return handleHttpsUrl(url)
}

πŸ’» Use Cases

  1. What do you want to use this for? Codebase with strict typings
  2. What shortcomings exist with current approaches? Has to either redefine the methods manually or use as
  3. What workarounds are you using in the meantime? Redefine the methods manually
@MartinJohns
Copy link
Contributor

Duplicate of #46958.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Mar 25, 2024
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Mar 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants