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.

Inverted `Promise` should warn like it does without inverting

See original GitHub issue

Suggestion

🔍 Search Terms

This condition will always return true since this ‘Promise<string | undefined>’ is always defined.(2801) Did you forget to use ‘await’?

✅ 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

When checking the inverted truthyness if something returned from an async function without calling await, it should warn you you forgot await. This works without inverting.

📃 Motivating Example

async function foo(): Promise<string | undefined> {
    return
}

const value = foo()
if (!value) { // <-- fails silently
    // ...
}

const awaitedValue = await foo()
if (awaitedValue) { // <-- Emits ts2801
    // ...
}

Playground

💻 Use Cases

In my use, I had a function that returned a User | undefined and I was doing something if there wasn’t a user returned. Later, I made the function async and forgot to update this since it was failing silently.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:3
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
Miodeccommented, May 8, 2022

Any update? Just ran into this issue. Tried 4.6.4 and its still a problem.

The bug I found today:

...
  if (!isNameAvailable(name)) {
    throw new MonkeyError(409, "Username already taken", name);
  }
...

isNameAvailable returns a promise, and so the throw line will never execute.

Removing the inversion highlights this error correctly:

if (isNameAvailable(name)) {
This condition will always return true since this 'Promise<boolean>' is always defined.ts(2801)
user.ts(54, 7): Did you forget to use 'await'?

Another interesting fact is that using === also highlights this error correctly:

if (!isNameAvailable(name) === true) {
This condition will always return 'false' since the types 'false' and 'true' have no overlap.ts(2367)

But, as mentioned, the first code block is silently always false, causing headaches 😄

1reaction
andrewbranchcommented, Sep 30, 2021

Yeah, there are several issues asking us to be more aggressive about either recognizing code as unreachable or issuing errors when we already do (like here—value is successfully narrowed to never inside if (!value), but the user has no way of noticing that because they think they just detected it to be undefined, so of course they’re not going to try to use it). But I like this issue because the scope is very narrow, and seems to sit well within the unawaited Promise / uncalled function checks we already have.

Also related is #45267

Read more comments on GitHub >

github_iconTop Results From Across the Web

The inverted yield curve explained and what it means for your ...
An inverted yield curve means interest rates have flipped on U.S. Treasurys with short-term bonds paying more than long-term bonds.
Read more >
node.js - Handling simple value rejects in a promise library - Stack ...
In the example above we are going to see the same Warning: a promise was rejected with a non-error again, which in this...
Read more >
The secret to a no-kick pole invert (and 3 exercises to get you ...
For beginner polers, it's usually the first pole trick that can elicit actual gasps and firework-display-worthy “oooos” from non-pole friends. And successfully ...
Read more >
Invert colours when notification/warning - Microsoft Community
I don't like it and I want to turn the setting off but I don't find it. Pls help. This thread is locked....
Read more >
25. Promises for asynchronous programming - Exploring JS
No inversion of control : similarly to synchronous code, Promise-based functions ... a Promise then that Promise is like a blank into which...
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