question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow wrapped values to be used in place of primitives.

See original GitHub issue
class 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:open
  • Created 9 years ago
  • Reactions:81
  • Comments:35 (4 by maintainers)

github_iconTop GitHub Comments

55reactions
RyanCavanaughcommented, Mar 17, 2015

It was an internal discussion before we went public.

Date is insane and I think why we backed off from doing this. Its valueOf method produces a number, but its [[DefaultValue]] internal method defaults to a string hint type (http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.8). This results in very surprising behavior:

> var x = new Date();
undefined
> x.valueOf()
1426616370842
> x + x
"Tue Mar 17 2015 11:19:30 GMT-0700 (Pacific Daylight Time)Tue Mar 17 2015 11:19:30 GMT-0700 (Pacific Daylight Time)"
> x - x
0
> x + 0
"Tue Mar 17 2015 11:19:30 GMT-0700 (Pacific Daylight Time)0"
> x - 0
1426616370842
> x * x / x
1426616370842
> x * x + x
"2.0352342695543988e+24Tue Mar 17 2015 11:19:30 GMT-0700 (Pacific Daylight Time)"

😢

14reactions
HerbCaudillcommented, Apr 8, 2019

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found