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

A second argument in Promise.all([...]) that returns a Record breaks the return signature #34924

Closed
dannycochran opened this issue Nov 5, 2019 · 7 comments · Fixed by #45350
Labels
Duplicate An existing issue was already created Fix Available A PR has been opened for this issue

Comments

@dannycochran
Copy link

dannycochran commented Nov 5, 2019

TypeScript Version:

3.7.2

Search Terms:

Promise.all, Record

Code

interface MoviesResponse {
    movies: [];
}

type CharactersResponse = Record<number, any>;

const getMovies = async (): Promise<MoviesResponse> => ({
    movies: [],
})

const getCharacters = async (): Promise<CharactersResponse> => ({
});

(async () => {
    const [{ movies }, characters] = await Promise.all([getMovies(), getCharacters()]);
    console.log(movies, characters);
})();

Expected behavior:

The first variable in the deconstructed array should be correctly typed as MoviesResponse, rather than CharactersResponse. This worked in 3.6.4.

Actual behavior:

Both variables in the deconstructed array are inferred to be CharactersResponse. So when I try to deconstruct the first argument doing { movies }, it's an invalid destructuring.

Interestingly, if we update CharactersResponse to not be a map type, the problem goes away, e.g:

interface CharactersResponse {
    characters: any[];
}
const getCharacters = async (): Promise<CharactersResponse> => ({
    characters: [],
});

Playground Link:

http://www.typescriptlang.org/play/?ssl=1&ssc=1&pln=17&pc=6#code/JYOwLgpgTgZghgYwgAgLIHsBuwIGcBKeADuiLigN4BQytyAtljrgFzIDaAugNxUC+VKmACeRFAGEAFnCiJIUAsVLlkAXmSEE6KABMAPCACu9AEbQANMjghhAPl5UtZMMgDmEMBmx41V3MJAEZAAKAEo2AAUodHpgcj0vZkJcEjIIWzUM4Oo6BiY8Ni5zflDBJ1wXdzApGTloXF84f0CQ8OQomLiIPRrZBHlFFOV0zJDqPlCHYKaAoLDRnLpyl3YKPO8GvksEaT6BzkaAdzhgFw7Y8gA6OAAbG+D2KsS8MMsq3rqFMM5JmiXldA3CCXG7oVzBRgbba7T64X4TMLcIA

Related Issues:

None found.

@nmain
Copy link

nmain commented Nov 5, 2019

Both movies and characters are inferred to be CharactersResponse

But the playground you linked to types movies as any (with an error)?

@dannycochran
Copy link
Author

Both movies and characters are inferred to be CharactersResponse

But the playground you linked to types movies as any (with an error)?

Sorry, you're right. It's that the first argument coming back from Promise.all is typed as CharactersResponse, so when I try to deconstruct it, it shows up as any. Will update my comment.

@nmain
Copy link

nmain commented Nov 5, 2019

I see. Once the example is corrected, this appears to work as you'd like it to in 3.6.3, but not 3.7.2

@orta
Copy link
Contributor

orta commented Nov 5, 2019

Maybe fixed by #33707

@AnyhowStep
Copy link
Contributor

AnyhowStep commented Nov 5, 2019

This will be fixed by #33707
I just tested

#33707 (comment)

@andrewbranch andrewbranch added Bug A bug in TypeScript Fix Available A PR has been opened for this issue labels Dec 2, 2019
@andrewbranch
Copy link
Member

Actually, this is a specific reproduction of #34937. This one came first by some number of minutes or hours, but the other has more discussion, so I’m going to mark this one duplicate—nothing personal @dannycochran 😄

@andrewbranch andrewbranch added Duplicate An existing issue was already created and removed Bug A bug in TypeScript Fix Available A PR has been opened for this issue labels Dec 2, 2019
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants