Skip to content

When using generic pick, object deconstruction does not provide valid keys to select #45663

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

Closed
txantslusam opened this issue Aug 31, 2021 · 3 comments · Fixed by #45719 or #49086
Closed
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue Rescheduled This issue was previously scheduled to an earlier milestone

Comments

@txantslusam
Copy link

Bug Report

🔎 Search Terms

Generic pick on object, type part of object, deconstructing of pick shows all keys of object

🕗 Version & Regression Information

  • This is a crash
  • This first occured today when trying to make some generic object picker
  • It was tested on TS 4.3.5 and 4.4.2
  • It occures both in TypescriptPlayground and Visual Studio Code
  • It decrease developer experience as deconstruction can't be used

⏯ Playground Link

Playground link with relevant code

💻 Code

// We can quickly address your report if:
//  - The code sample is short. Nearly all TypeScript bugs can be demonstrated in 20-30 lines of code!
//  - It doesn't use external libraries. These are often issues with the type definitions rather than TypeScript bugs.
//  - The incorrectness of the behavior is readily apparent from reading the sample.
// Reports are slower to investigate if:
//  - We have to pare too much extraneous code.
//  - We have to clone a large repo and validate that the problem isn't elsewhere.
//  - The sample is confusing or doesn't clearly demonstrate what's wrong.
interface Mapper<T = any, R = any> {
    (arg: T): R;
}

function pick<O, T extends keyof O>(keys: T[], obj?: O): Pick<O, T>;
function pick<T>(keys: T[]): Mapper;
function pick<O, T extends keyof O>(keys: T[], obj?: O) {
    const picker: Mapper<O, { [K in T]: O[T] }> = _obj => Object.entries(_obj).reduce((o, [key, val]) => {
        if (keys.includes(key as T)) o[key as T] = val;
        return o;
    }, {} as Pick<O, T>);

    return obj ? picker(obj) : picker;
}

const { a, b } = pick(['b'], { a: 'a', b: 'b' }) //here the autosuggest should show b only
const obj = pick(['b'], { a: 'a', b: 'b' })
obj.a // here it is working properly as you can only select b

🙁 Actual behavior

When using the deconstruction construction after pick, I can select b as well as a. But the a is not in object returned from pick.
When first assigning the return value to const, in later code I can only select b, which is expected.

🙂 Expected behavior

In deconstruction I should be able to select only keys, that I declared in pick function. The behaviour should be same as in second example, where the return object is first assigned to obj var and then refered.

@andrewbranch
Copy link
Member

I think this is actually just a symptom of #43605

@andrewbranch andrewbranch added the Needs Investigation This issue needs a team member to investigate its status. label Sep 2, 2021
@andrewbranch andrewbranch self-assigned this Sep 2, 2021
@andrewbranch andrewbranch added this to the TypeScript 4.5.0 milestone Sep 2, 2021
@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Sep 3, 2021
@txantslusam
Copy link
Author

Thanks @andrewbranch. It is working on nightly build on playground 🎉

@andrewbranch
Copy link
Member

Unfortunately the fix wasn't quite right and we decided to revert it and investigate more for next release—see #46009 and #46012

@andrewbranch andrewbranch reopened this Sep 22, 2021
@andrewbranch andrewbranch added Bug A bug in TypeScript and removed Needs Investigation This issue needs a team member to investigate its status. Fix Available A PR has been opened for this issue labels Sep 22, 2021
@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label May 12, 2022
@RyanCavanaugh RyanCavanaugh added the Rescheduled This issue was previously scheduled to an earlier milestone label May 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue Rescheduled This issue was previously scheduled to an earlier milestone
Projects
None yet
4 participants