Inverted `Promise` should warn like it does without inverting
See original GitHub issueSuggestion
🔍 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
// ...
}
💻 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:
- Created 2 years ago
- Reactions:3
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Any update? Just ran into this issue. Tried 4.6.4 and its still a problem.
The bug I found today:
isNameAvailable
returns a promise, and so the throw line will never execute.Removing the inversion highlights this error correctly:
Another interesting fact is that using
===
also highlights this error correctly:But, as mentioned, the first code block is silently always false, causing headaches 😄
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 tonever
insideif (!value)
, but the user has no way of noticing that because they think they just detected it to beundefined
, 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