Skip to content

Migrate String to StringPrototype, Number to NumberPrototype, etc #55175

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

Open
5 tasks done
RyanCavanaugh opened this issue Jul 27, 2023 · 5 comments
Open
5 tasks done
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@RyanCavanaugh
Copy link
Member

Suggestion

πŸ” Search Terms

string prototype object number stringprototype

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

We're always seeing people writing String, Number, Object, and so on, in places where they should be writing their primitive equivalents.

We should mark these as @deprecated so that they show up with strikethroughs in the editor, and make new global interfaces named e.g. StringPrototype so it's more clear what they're for.

/**
 * @deprecated Use `string` when working with strings, or use `StringPrototype` to declare new string methods
 */
interface String {

For back compat, possibly forever, we'll need to declare these as

interface StringPrototype extends String { }

so that existing references to String["charAt"] continue to work and so that other merges continue to function.

πŸ“ƒ Motivating Example

image

image

πŸ’» Use Cases

  • New users coming to TS
@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Jul 27, 2023
@RyanCavanaugh
Copy link
Member Author

@DanielRosenwasser suggested also removing them from the global completion list, which also seems like a good idea

@MartinJohns
Copy link
Contributor

For back compat, possibly forever

Consider removing it in the future, perhaps as part of 6.0? #54500

@fatcerberus
Copy link

fatcerberus commented Jul 27, 2023

I don't know if they can ever be removed completely, because otherwise what does new String("foo") return? (not that anyone should be doing that, but it's valid JS, so...)

@MartinJohns
Copy link
Contributor

@fatcerberus Valid point, and I'd say: A compiler error. If people really want to do that, they can add the declaration themselves.

@RyanCavanaugh
Copy link
Member Author

String the global variable stays put, of course. This is just about the interface

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants