-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Promise intersection with primitive results in wrong awaited type #48927
Comments
#45350 this is probably related. However, as shown before, the |
I don’t understand (even with your playground example) why you would ever write |
The code in there seems to be using Even better, could you show a working example of your use case at runtime? Your playground link uses |
It does seem theoretically wrong, but yeah, unless there is a real-world use case I doubt we’ll prioritize a change here. |
I don't think this would be a hard fix - at the moment this is somewhat explicitly called out to be invalid in one of the comments: Probably one of the required changes to make this work would be to tweak the related logic in the I could take a stab at this - if only somebody would confirm that this behavior needs to be adjusted. I also don't see though how this could be utilized in the real-world code so far. |
I did a quick experiment monkeypatching
The only way I can interpret |
Thank you all for your feedback. @andrewbranch I don't think this issue should be closed. I just made a full example use case which roughly replicates what I'm trying to achieve in my project. I'm working on a custom asynchronous store for Svelte similar to this one or this one. The idea behind it is to abstract away API interactions, so the store can manage everything. To achieve that I'm using a data structure which I call This has one caveat though. When we first look at out rich promise (before any api calls resolve), there is no data to be concatenated with a promise, so we pair it with This whole approach was chosen to elegantly support 2 different rendering strategies in svelte templates: <!-- Strategy 1 -->
<p>
{#if $store != loading}
{$store}
{:else}
<!-- Is shown only the first time -->
Loading
{/if}
</p>
<!-- Strategy 2 -->
<p>
{#await $store}
<!-- Is shown every time new data starts loading -->
Loading
{:then data}
{data}
{/await}
</p> In the linked example I'm trying to mimic something similar to svelte stores without the framework. If you run the code, you'll see the showcases of the two strategies shown above:
Note that in a real scenario the same data would not be rendered twice. Anyway besides all of that, this is INCONSISTENCY (between |
Also, if we had a way to data: A | B;
if (data != B) {
data: A;
} where If anyone knows if the snippet above is achievable (without changing an operator or using a function) please let me know. Fun factI also tried to use {#if $store}
{$store}
{:else}
Loading
{/if}
{#await $store}
Loading
{:then data}
{data}
{/await} But it was a little bit to far... And there were some problems with promises not being detected since |
And even ignoring everything considered, it just makes sense for |
Then what you have is a
This distinction matters because: const x: string = new String("foo"); // error TS2322
// Type 'String' is not assignable to type 'string'. |
@andrewbranch Yeah... I think you are right after all. I did use an But what I actually want is to have const singleton = {} as unique Record<string, any>;
const unknown = {a: "test"} as {a: string} | typeof singleton.
if (unknown != singleton) {
unknown: {a: "test"};
unknown.a; //test
} If there's already a way to do this in the language, please let me know. If not, I'll open a feature request then. |
|
Bug Report
When we make an intersection of a promise with a primitive type like
Promise<T> & T
(where T is primitive (non-object)), the resulting type fromawait (Promise<T> & T)
is wrong (still contains a promise). This is clearly a bug sinceAwaited<Promise<T> & T>
and(Promise<T> & T).then(x => {})
behave correctly (the result is onlyT
). The issue is only observed when using theawait
keyword.The types arrangement might seem arbitrary, but it has a valid use case which is provided in the playground link below.
🔎 Search Terms
PROMISE & PRIMITIVE
,PROMISE INTERSECTION
,AWAITED TYPE
,INCONSITENT THEN AND AWAIT
🕗 Version & Regression Information
4.5.5
and4.7.0-dev.20220503
.4.4.4
and lower are unaffected by this bug.⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
Type
B
still containsPromise<number>
despite of the value being awaited.🙂 Expected behavior
In the example above
B
has to be equal toC
andD
.The text was updated successfully, but these errors were encountered: