Skip to content

Infer "stringable" or "numerable" objects without explicit declaration #61477

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
nexusprime-here opened this issue Mar 24, 2025 · 2 comments
Closed
6 tasks done
Labels
Duplicate An existing issue was already created

Comments

@nexusprime-here
Copy link

🔍 Search Terms

TypeScript infer toString valueOf type

TypeScript primitive coercion support

TypeScript object implicit conversion

TypeScript toString type inference

TypeScript valueOf type inference

TypeScript custom primitive type support

TypeScript implicit type conversion issue

TypeScript Symbol.toPrimitive inference

✅ Viability Checklist

⭐ Suggestion

Allow the compiler to infer that objects with consistently implemented toString() or valueOf() methods can be used directly as string or number without requiring explicit conversion, like is on javascript.

Benefits:

  • Reduces verbosity and the need for manual conversion.
  • Improves developer experience when working with objects that can behave as primitives.

Thank you for your consideration!

📃 Motivating Example

Currently, TypeScript does not automatically infer that objects with custom toString() or valueOf() methods can be used as string or number. This forces developers to perform explicit conversions, which can make the code more verbose.

Example:

class MyObject {
  valueOf() {
    return true;
  }
}

const obj = new MyObject();
if (!obj) {...}  // invalid

💻 Use Cases

  1. What do you want to use this for?
    I want TypeScript to infer when an object can behave as a string or number based on its toString() or valueOf() methods. This would allow objects with well-defined conversions to be used more naturally without requiring explicit casts or manual conversions.
  2. What shortcomings exist with current approaches?
    Currently, TypeScript does not respect toString() or valueOf() for type inference. Even if an object defines these methods correctly, the compiler still treats it as its declared type rather than allowing implicit conversion. This leads to unnecessary type assertions or explicit calls, making the code more verbose.
  3. What workarounds are you using in the meantime?
  • Manually calling .toString() or .valueOf() when needed.
  • Using type assertions (as string or as number), which can be unsafe.
  • Wrapping objects in utility functions to handle conversions explicitly.
@MartinJohns
Copy link
Contributor

Duplicate of #2361.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Mar 24, 2025
@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 27, 2025
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