faker icon indicating copy to clipboard operation
faker copied to clipboard

array-like elements have default arguments which violate genericity

Open damienwebdev opened this issue 3 years ago • 6 comments

Describe the bug

I was reviewing https://github.com/faker-js/faker/pull/189/files and I noticed that randomize supports generic results, but there's an issue with the arguments having a default.

Consider:

const a = randomize<number>();

This would return a string, which violates the type.

Reproduction

TypeScript Playground Link

Additional Info

No response

damienwebdev avatar Jan 18 '22 14:01 damienwebdev

I assume the only way to really fix this, is to fully remove the default and force an argument to be passed in. This would be a breaking change.

Shinigami92 avatar Jan 18 '22 14:01 Shinigami92

It seems the violation happens when the argument is undefined / empty. Are we trying to achieve

const a: number = randomize<number>();
console.log(a); // 1

const b = randomize();
console.log(b); // "a"

or else we may have to think of other ways

poyea avatar Jan 18 '22 17:01 poyea

It seems the violation happens when the argument is undefined / empty. Are we trying to achieve

const a: number = randomize<number>();
console.log(a); // 1

const b = randomize();
console.log(b); // "a"

or else we may have to think of other ways

Yeah, but sadly this is not possible due to TS is only at compile time. So the generic cannot change runtime behavior :shrug: So we may just remove the default value for the parameter and then declare it as breaking change

Shinigami92 avatar Jan 18 '22 19:01 Shinigami92

It seems the violation happens when the argument is undefined / empty. Are we trying to achieve

const a: number = randomize<number>();
console.log(a); // 1

const b = randomize();
console.log(b); // "a"

or else we may have to think of other ways

Yeah, but sadly this is not possible due to TS is only at compile time. So the generic cannot change runtime behavior 🤷 So we may just remove the default value for the parameter and then declare it as breaking change

Yes if we've to change it. At least, generic specialization in TS is not possible.

poyea avatar Jan 18 '22 19:01 poyea

This function is marked as deprecated (for removal in v7) and we already want to get rid of the default ['a', 'b', 'c'] (#589) So I'm not sure if it's worth working on this issue specifically

Maybe we should just close this issue?

Shinigami92 avatar Mar 23 '22 17:03 Shinigami92

Isn't #589 the fix for this issue?

ST-DDT avatar Mar 23 '22 23:03 ST-DDT

Blocked by #893

xDivisionByZerox avatar Jan 29 '23 17:01 xDivisionByZerox

Team Decision

Can be implemented without #893

We want this for v8.0

Shinigami92 avatar Apr 13 '23 16:04 Shinigami92