Allow wrapped values to be used in place of primitives.
See original GitHub issueclass NumberWrapper {
constructor(private value: number) {}
valueOf(): number { return this.value; }
}
var x = new NumberWrapper(1);
// The right-hand side of an arithmetic operation
// must be of type 'any', 'number' or an enum type.
console.log(2 + x);
It would be nice if an arithmetic operation could allow using the wrapped number because it’s valueOf()
method returns the expected primitive type.
This can be generalized to: if type T
is expected, then a value that implements the following interface can be used.
interface Wrapped<T> {
valueOf(): T;
}
Issue Analytics
- State:
- Created 9 years ago
- Reactions:81
- Comments:35 (4 by maintainers)
Top Results From Across the Web
Java: Understanding Primitive Types and Wrapper Objects
All primitive wrapper objects in Java are final, which means they are immutable. When a wrapper object get its value modified, the compiler...
Read more >What are wrapper objects for primitive values?
All wrapper classes can be function-called, which converts an arbitrary value to the primitive type that the class represents. This is a ...
Read more >When should I use primitives instead of wrapping objects?
When a boxing occurs for a well-known value, instead of creating a new wrapper instance, a pre-created instance is fetched from a pool...
Read more >Wrapping Primitives to Enable Returning null -- Bad ...
One technique that I sometimes use in C/C++ is to have the function return a boolean value indicating whether the function was able...
Read more >Why Java Collections Cannot Directly Store Primitives ...
Wrapper Class provides a way to use primitive data types (int, boolean, etc..) as objects or a Wrapper class is a class whose...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
It was an internal discussion before we went public.
Date
is insane and I think why we backed off from doing this. ItsvalueOf
method produces anumber
, but its[[DefaultValue]]
internal method defaults to astring
hint type (http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.8). This results in very surprising behavior:😢
@RyanCavanaugh this strikes me as a bug. TypeScript shouldn’t choke on valid JavaScript. It seems weird that this issue is still open after 4 years, especially considering that the community has provided multiple use cases, proof-of-concept code, and a specification.