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

faker.helpers.objectKey returns wrong type #2718

Closed
9 of 10 tasks
ST-DDT opened this issue Mar 5, 2024 · 3 comments
Closed
9 of 10 tasks

faker.helpers.objectKey returns wrong type #2718

ST-DDT opened this issue Mar 5, 2024 · 3 comments
Labels
c: bug Something isn't working m: helpers Something is referring to the helpers module wontfix This will not be worked on

Comments

@ST-DDT
Copy link
Member

ST-DDT commented Mar 5, 2024

Pre-Checks

Describe the bug

When calling faker.helpers.objectKey with a Record<number, *> it declares to return a number, but actually returns a string.

I discovered this, when I tried to pass a mapped type to the function and got string | number back.

const object: { [a: string]: '1' } = { a: '1' };
const value: string = faker.helpers.objectKey(object); // 🧨Cannot assign `number | string` to `string`
console.log(value, typeof value); // a string

Minimal reproduction code

const object: Record<number, number> = { 1: 1 };
const value: number = faker.helpers.objectKey(object);
console.log(value, typeof value); // 1 string

Additional Context

This seems to be intentional on the TypeScript side.
https://stackoverflow.com/questions/51808160/keyof-inferring-string-number-when-key-is-only-a-string

Environment Info

Binaries:
    Node: 20.11.1 - C:\Program Files\nodejs\node.EXE
    pnpm: 8.12.1 - C:\Program Files\nodejs\pnpm.CMD
  npmPackages:
    @faker-js/faker: 8.4.1 => 8.4.1

Which module system do you use?

  • CJS
  • ESM

Used Package Manager

pnpm

@ST-DDT ST-DDT added c: bug Something isn't working s: needs decision Needs team/maintainer decision m: helpers Something is referring to the helpers module labels Mar 5, 2024
@Shinigami92
Copy link
Member

Shinigami92 commented Mar 5, 2024

For me it shows this (next-branch):

image image

@ST-DDT
Copy link
Member Author

ST-DDT commented Mar 5, 2024

You have to use a mapped type explicitly as shown in the minimal reproduction code.

And for the later one, if you run console.log(typeof x) then it returns string and not number.

@ST-DDT
Copy link
Member Author

ST-DDT commented Apr 4, 2024

Team Decision

  • "This seems to be intentional on the TypeScript side."

@ST-DDT ST-DDT closed this as not planned Won't fix, can't repro, duplicate, stale Apr 4, 2024
@ST-DDT ST-DDT added wontfix This will not be worked on and removed s: needs decision Needs team/maintainer decision labels Apr 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: bug Something isn't working m: helpers Something is referring to the helpers module wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants