Skip to content
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

Allow objects to be passed to BigIntConstructor. #46636

Closed
12wrigja opened this issue Nov 2, 2021 · 3 comments
Closed

Allow objects to be passed to BigIntConstructor. #46636

12wrigja opened this issue Nov 2, 2021 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@12wrigja
Copy link

12wrigja commented Nov 2, 2021

lib Update Request

Missing / Incorrect Definition

BigIntConstructor does not accept object, which it should as the spec defines it as being able to convert any object to a primitive.

Sample Code

globalThis.BigInt({
  valueOf: () => 20,
});

Documentation Link

https://tc39.es/ecma262/#sec-bigint-constructor-number-value

@MartinJohns
Copy link
Contributor

Essentially a duplicate of #2361.

@andrewbranch andrewbranch added the Duplicate An existing issue was already created label Nov 3, 2021
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@12wrigja
Copy link
Author

12wrigja commented Nov 8, 2021

I can see how that is related, but I don't think it is a duplicate.

That logic does make sense for JS operators, where the coercion is reasonably well defined. That doesn't apply to the types in the BigInt constructor though. In the more general case, I don't think it's correct to change TS to allow wrapped objects everywhere that the valueOf return type matches a given type. See this playground for some examples where this doesn't work.

I would hope that if this behavior was desirable for authors of a function they would be able to opt into it explicitly (strawman: function example(x: number|HasValueOf<number>) where HasValueOf<T> matches any object whose valueOf returns type T). If this were the case, I'd expect the BigInt typings to be updated to include it, but as it stands today that doesn't exist and object is the best that can be done.

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