Skip to content

faker.date.betweens should return a fixed-length tuple type rather than array of Date #3407

@fnimick

Description

@fnimick

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

Metadata

Metadata

Labels

c: featureRequest for new featurehas workaroundWorkaround provided or linkedm: dateSomething is referring to the date modulep: 1-normalNothing urgent

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions