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.date.betweens should return a fixed-length tuple type rather than array of Date #3407

Open
9 of 10 tasks
fnimick opened this issue Feb 21, 2025 · 2 comments
Open
9 of 10 tasks
Labels
c: feature Request for new feature has workaround Workaround provided or linked m: date Something is referring to the date module p: 1-normal Nothing urgent s: awaiting more info Additional information are requested

Comments

@fnimick
Copy link

fnimick commented Feb 21, 2025

Pre-Checks

Describe the bug

faker.date.betweens returns a Date[], which means:

  • if typescript option noUncheckedIndexedAccess is set, all indexed values are Date | undefined
  • if noUncheckedIndexAccess is not set, it is possible to index beyond the end of the array and have undefined at runtime, while the compile-type type is Date.

Ideally, faker.date.betweens should return a fixed-length tuple based on the provided count, defaulting to [Date, Date, Date] to match the default count of 3.

Minimal reproduction code

// noUncheckedIndexAccess is not set for this example
const fakeDates = faker.date.betweens({
from: "2020-01-01T00:00:00.000Z",
to: "2020-04-01T00:00:00.000Z",
count: 3,
});
let [a, b, c, d] = fakeDates;
// d is Date for type-checking, but undefined at runtime

const fakeDatesWithType = fakeDates as [Date, Date, Date];
// this is now a type error on h
let [e, f, g, h] = fakeDatesWithType;

Additional Context

No response

Environment Info

System:
    OS: macOS 15.3.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 96.45 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.14.0 - ~/.asdf/installs/nodejs/22.14.0/bin/node
    npm: 10.9.2 - ~/.asdf/installs/nodejs/22.14.0/bin/npm
    pnpm: 9.15.5 - ~/.asdf/shims/pnpm
  Browsers:
    Chrome: 133.0.6943.127
    Safari: 18.3
  npmPackages:
    @faker-js/faker: ^9.0.3 => 9.5.0

Which module system do you use?

  • CJS
  • ESM

Used Package Manager

npm

@fnimick fnimick added c: bug Something isn't working s: pending triage Pending Triage labels Feb 21, 2025
@ST-DDT
Copy link
Member

ST-DDT commented Feb 21, 2025

Possible duplicate of:

IMO this is a limitation of typescript that we hardly can avoid. We could use recursive types but these are expensive and I consider them a non-option. I also dont like to add 10+ signatures to a method to cover all possible array lengths.

Are there other solutions to this?


As a workaround you can cast the result to a tuple.


What are the common lengths of betweens that you use and what for?

@ST-DDT ST-DDT added c: feature Request for new feature p: 1-normal Nothing urgent s: awaiting more info Additional information are requested m: date Something is referring to the date module has workaround Workaround provided or linked and removed c: bug Something isn't working s: pending triage Pending Triage labels Feb 21, 2025
@fnimick
Copy link
Author

fnimick commented Feb 22, 2025

We could use recursive types but these are expensive and I consider them a non-option.

How expensive? I have used recursive types up to 70 long in my codebase with no perceptible performance penalty.

I use this for creating a series of fake dates in order and assigning them to variables, e.g. const [a, b, c....] = faker.date.betweens(...). Having a fixed length tuple as the result ensures that we never assign a variable that is undefined at runtime. Of course this would only apply when count is a number, and not a range.

I am currently casting to ReadonlyTuple from https://github.com/sindresorhus/type-fest/blob/main/source/readonly-tuple.d.ts .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: feature Request for new feature has workaround Workaround provided or linked m: date Something is referring to the date module p: 1-normal Nothing urgent s: awaiting more info Additional information are requested
Projects
None yet
Development

No branches or pull requests

2 participants