Skip to content

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

Open
@RyanCavanaugh

Description

@RyanCavanaugh

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions