You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
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.
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.
The text was updated successfully, but these errors were encountered:
🔍 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()
orvalueOf()
methods can be used directly asstring
ornumber
without requiring explicit conversion, like is on javascript.Benefits:
Thank you for your consideration!
📃 Motivating Example
Currently, TypeScript does not automatically infer that objects with custom
toString()
orvalueOf()
methods can be used asstring
ornumber
. This forces developers to perform explicit conversions, which can make the code more verbose.Example:
💻 Use Cases
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.
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.
The text was updated successfully, but these errors were encountered: