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

Types when passing { deep: true } are wrong #22

Open
felixfbecker opened this issue Aug 1, 2019 · 2 comments
Open

Types when passing { deep: true } are wrong #22

felixfbecker opened this issue Aug 1, 2019 · 2 comments

Comments

@felixfbecker
Copy link

The types type the key and value arguments as keys and values of the passed root object, but when passing { deep: true }, my understanding is these could be deeply nested keys and values (i.e. key: string and value: unknown)

@sindresorhus
Copy link
Owner

I'm not sure exactly what you mean. Would be great if you could submit a failing type test: https://github.com/sindresorhus/map-obj/blob/master/index.test-d.ts (expectError) or also a fix.

@sscotth
Copy link

sscotth commented Nov 5, 2021

Possible example:

const input = { foo: { bar: 'baz' }, hello: 'world' }

const output = mapObject(
  input,
  (key, value) =>
    // args with `{deep: true}` shouldn't use `keyof`. In this case `keyof input` is `'"foo" | "hello"'`
    // @ts-expect-error
    key === 'bar' ? mapObjectSkip : [key, value],
  { deep: true },
)

expect(output).toStrictEqual({ foo: {}, hello: 'world' })

Without the expect-error:

This condition will always return 'false' since the types '"foo" | "hello"' and '"bar"' have no overlap.

The problem exists for keys and values

map-obj/index.d.ts

Lines 23 to 24 in 5047229

sourceKey: keyof SourceObjectType,
sourceValue: SourceObjectType[keyof SourceObjectType],

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

No branches or pull requests

3 participants