Skip to content

chore: improve typings on random.objectElement #407

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

Merged
merged 8 commits into from
Feb 3, 2022

Conversation

pkuczynski
Copy link
Member

@pkuczynski pkuczynski commented Feb 3, 2022

No description provided.

@pkuczynski pkuczynski requested a review from a team as a code owner February 3, 2022 00:01
@pkuczynski pkuczynski marked this pull request as draft February 3, 2022 00:01
@pkuczynski pkuczynski marked this pull request as ready for review February 3, 2022 00:02
@ejcheng ejcheng added the c: chore PR that doesn't affect the runtime behavior label Feb 3, 2022
@Shinigami92 Shinigami92 marked this pull request as draft February 3, 2022 08:46
@Shinigami92
Copy link
Member

@pkuczynski could you try:

  objectElement<T extends Record<string, unknown>, K extends keyof T>(
    object: T,
    field: 'key'
  ): K;
  objectElement<T extends Record<string, unknown>, K extends keyof T>(
    object: T,
    field?: unknown
  ): T[K];
  objectElement<T extends Record<string, unknown>, K extends keyof T>(
    object = { foo: 'bar', too: 'car' } as unknown as T,
    field = 'value'
  ): K | T[K] {
    const array: Array<keyof T> = Object.keys(object);
    const key = this.faker.random.arrayElement(array);

    return field === 'key' ? (key as K) : (object[key] as T[K]);
  }

@pkuczynski pkuczynski marked this pull request as ready for review February 3, 2022 21:18
@Shinigami92 Shinigami92 requested review from ST-DDT and a team February 3, 2022 21:21
@Shinigami92 Shinigami92 merged commit 1c8747d into faker-js:main Feb 3, 2022
@pkuczynski pkuczynski deleted the random-object-element-type branch February 3, 2022 21:42
@xDivisionByZerox
Copy link
Member

Wouldn't it be much easier from an architectural and types perspective, by splitting this method into 2 separate ones? One for object value and one for object key.

demipel8 pushed a commit to demipel8/faker that referenced this pull request Mar 11, 2022
@ST-DDT ST-DDT removed this from the v6 - Non-Breaking Changes milestone Apr 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: chore PR that doesn't affect the runtime behavior
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants