8000 Using omit can't validate key passed as parameter · Issue #5884 · lodash/lodash · GitHub
[go: up one dir, main page]

Skip to content

Using omit can't validate key passed as parameter #5884

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

Open
BIG293 opened this issue Jun 12, 2024 · 5 comments
Open

Using omit can't validate key passed as parameter #5884

BIG293 opened this issue Jun 12, 2024 · 5 comments

Comments

@BIG293
Copy link
BIG293 commented Jun 12, 2024

It seems that generic omit overloads prevent the usage of the typed one that connect the object type to the type of keys

import { omit } from 'lodash-es'

interface A {
  a: number;
  b: string;
}

const a: A = {
  a: 1,
  b: '2'
}

omit(a, 'c') // it compiles, but it should not
@avivkal
Copy link
avivkal commented Jun 14, 2024

It seems like the current type is string | number | symbol, you can certainly make it more specific by yourself, for example:
omit<A, keyof A>(a, 'ad') // this will raise a compilation error

@BIG293
Copy link
Author
BIG293 commented Jun 14, 2024

I certainly know that I can do it, but then I cannot use Typescript's type inference adding boilerplate and making that overload useless. It would be enough one overload with any or string | number | symbol as default

@avivkal
Copy link
avivkal commented Jun 14, 2024

I am not sure I understood, do you mean something like this:
omit<keyof A>(a, ['a'])
Or something like this:
omit(a, ['a'])
that will always enforce the type?

@BIG293
Copy link
Author
BIG293 commented Jun 14, 2024

No. The problem is that the omit has three overloads:

        omit<T extends object, K extends PropertyName[]>(
            object: T | null | undefined,
            ...paths: K
        ): Pick<T, Exclude<keyof T, K[number]>>;
        /**
         * @see _.omit
         */
        omit<T extends object, K extends keyof T>(object: T | null | undefined, ...paths: Array<Many<K>>): Omit<T, K>;
        /**
         * @see _.omit
         */
        omit<T extends object>(object: T | null | undefined, ...paths: Array<Many<PropertyName>>): PartialObject<T>;

The problem is that I'd want to use the second one, without explicitly passing the generics parameters, leveraging the TS's type inference. At the moment with this three overload instead you don't have any intellisense and if you pass any key that is not in the object keys it compiles. Therefore this solution is not robust and expose you to risk in case of refactoring of the property names.

@NinaRoss1128
Copy link

Where for I omit that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants
0