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.

Prevent spreading of promises, as it's likely not intended

See original GitHub issue

Suggestion

Spreading a promise in an object is correct Javascript, and Typescript allows it. But it doesn’t do anything:

{ ...Promise.resolve({ key: 42 }) } // returns {}

Any time you’re trying to spread a promise, it’s much more likely that you have an async function that you forgot to await. The following code compiles.

const myAsyncFunc = () => Promise.resolve({ key: 42 });

const result = {
  foo: 'bar',
  ...myAsyncFunc(),
}; 

While this is “correct”, it’s almost certainly not what the programmer intended.

🔍 Search Terms

Spreading promise.

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

(I’m not sure about the first point. This would be a breaking change, but it should only affect code that likely contains a bug. The feature request does support the goal of “Statically identify constructs that are likely to be errors.”)

⭐ Suggestion

Prevent code that spreads promises from compiling.

📃 Motivating Example

As it’s almost certainly unintended, Typescript should prevent promises from being spread in objects.

💻 Use Cases

This would prevent a bug that I personally faced recently. I can’t think of any scenario where someone would intentionally want to spread a promise.

An IDE or a plugin could detect that you’re spreading a function marked as async, but I’m not sure whether they would be able to detect that a function returns a promise, or that a function returns the result of another function call that returns a promise.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:3
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
RyanCavanaughcommented, May 18, 2022

I think the specific case of disallowing spreading a Promise is very unobjectionable – it’s extremely obviously a mistake, and a refactoring hazard when asyncifying a sync function.

A rule of “disallow spreading if there are zero enumerable properties” is overbroad, because people will sometimes write something like

const opts = useOptions ? options : { };
const x = { ...opts, foo: 3 }

wherein opts gets subtype-reduced to { } and thus has zero enumerable properties.

2reactions
falinskycommented, May 18, 2022

+1

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fact Check-Preventing transmission never required for COVID ...
The misleading posts imply that national restrictions such as vaccine passports were based on a promise of vaccines blocking virus spread that ...
Read more >
Final Rule: Selective Disclosure and Insider Trading - SEC.gov
Rule 100(b)(1) enumerates four categories of persons to whom selective disclosure may not be made absent a specified exclusion. The first three are...
Read more >
President Woodrow Wilson's 14 Points (1918)
In this January 8, 1918, speech on War Aims and Peace Terms, President Wilson set down 14 points as a blueprint for world...
Read more >
PENAL CODE CHAPTER 31. THEFT
(C) preventing another from acquiring information likely to affect his ... proof that the actor did not intend to perform or knew the...
Read more >
Mission, Role and Pledge | About - CDC
Centers for Disease Control and Prevention (CDC) - CDC's vision, mission, core values, ... CDC increases the health security of our nation.
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