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

optional argument that don't need to be specified #50657

Closed
5 tasks
jimmywarting opened this issue Sep 6, 2022 · 4 comments
Closed
5 tasks

optional argument that don't need to be specified #50657

jimmywarting opened this issue Sep 6, 2022 · 4 comments
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript

Comments

@jimmywarting
Copy link
Contributor

Suggestion

Some stuff have become to strict... that it's becoming too annoying!

await new Promise(rs => {
  setTimeout(() => rs(), 1000)
})
Expected 1 argument, but got 0. 'new Promise()' needs a JSDoc hint to produce
a 'resolve' that can be called without arguments

it's not only that. new ArrayBuffer() and new Uint8Array() is also something that requires an argument but it's perfectly fine to omit it to represent a 0 length buffer in the cases where you need a empty placeholder

🔍 Search Terms

promise resolve

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

📃 Motivating Example

💻 Use Cases

@jakebailey
Copy link
Member

Promise definitely requires an argument. If you do not pass it a function, it will throw.

It's true that at runtime, ArrayBuffer and Uint8Array() "work" when you don't pass an argument, but only because the parameter is then undefined, which is coerced to a number which is then zero. We could say the same about passing "0" or false. In general, we don't aim to make types so wide as to capture coercion behaviors; e.g., we do not parseInt(undefined) even though that "works" and returns NaN. The ArrayBuffer and Uint8Array constructors are all documented in the spec and MDN and such as having a required parameter, so I don't think it's a good idea to change this.

@jakebailey jakebailey added Suggestion An idea for TypeScript Declined The issue was declined as something which matches the TypeScript vision labels Sep 7, 2022
@jakebailey jakebailey closed this as not planned Won't fix, can't repro, duplicate, stale Sep 7, 2022
@jimmywarting
Copy link
Contributor Author

jimmywarting commented Sep 8, 2022

Promise definitely requires an argument. If you do not pass it a function, it will throw.

You got it wrong. i wasn't talking about the promise constructor. i was talking about the resolver's argument

@jimmywarting
Copy link
Contributor Author

and i didn't find anything on mdn about typedarray requireing an argument...

they even have an example of it here where it's omited:

image

@jakebailey
Copy link
Member

RE: Promise, we know that this isn't ideal; see #46570, #40505. What we'd need is some sort of concrete proposal that would help things.

RE: The constructors, we already have the overloads in place such that types like Uint8Array are legal without parameters. Playground Link

This isn't the case for ArrayBuffer, but it's really to just explicitly pass in 0 as the parameter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants