-
-
Notifications
You must be signed in to change notification settings - Fork 939
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(helpers)!: use const generics where possible (#2685)
- Loading branch information
Showing
5 changed files
with
165 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
### Usage of TypeScript 5 Features | ||
|
||
_This upgrade is an extension to_ [#1953](./1953.md) | ||
|
||
The helpers module now uses TS5 features, so if you are using Faker with TypeScript, you must use TS5. | ||
|
||
```ts | ||
// v8 | ||
faker.helpers.arrayElement([1, 2, 3]); // number | ||
faker.helpers.arrayElement([1, 2, 3] as const); // 1 | 2 | 3 | ||
|
||
// v9 | ||
faker.helpers.arrayElement([1, 2, 3]); // 1 | 2 | 3 | ||
``` | ||
|
||
If you are unable to upgrade to TS5, you have to keep using Faker v8. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import { describe, expectTypeOf, it } from 'vitest'; | ||
import { faker } from '../../src'; | ||
|
||
describe('helpers', () => { | ||
describe('shuffle', () => { | ||
describe('inplace: true', () => { | ||
it('const generic single element', () => { | ||
const actual = faker.helpers.shuffle([1], { inplace: true }); | ||
expectTypeOf(actual).toEqualTypeOf<Array<1>>(); | ||
}); | ||
|
||
it('const generic multiple elements', () => { | ||
const actual = faker.helpers.shuffle([1, 'a', false], { | ||
inplace: true, | ||
}); | ||
expectTypeOf(actual).toEqualTypeOf<Array<1 | 'a' | false>>(); | ||
}); | ||
}); | ||
|
||
describe('inplace: false', () => { | ||
it('const generic single element', () => { | ||
const actual = faker.helpers.shuffle([1], { inplace: false }); | ||
expectTypeOf(actual).toEqualTypeOf<Array<1>>(); | ||
}); | ||
|
||
it('const generic multiple elements', () => { | ||
const actual = faker.helpers.shuffle([1, 'a', false], { | ||
inplace: false, | ||
}); | ||
expectTypeOf(actual).toEqualTypeOf<Array<1 | 'a' | false>>(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('uniqueArray', () => { | ||
it('const generic single element', () => { | ||
const actual = faker.helpers.uniqueArray([1], 1); | ||
expectTypeOf(actual).toEqualTypeOf<Array<1>>(); | ||
}); | ||
|
||
it('const generic multiple elements', () => { | ||
const actual = faker.helpers.uniqueArray([1, 'a', false], 3); | ||
expectTypeOf(actual).toEqualTypeOf<Array<1 | 'a' | false>>(); | ||
}); | ||
}); | ||
|
||
describe('maybe', () => { | ||
it('const generic single element', () => { | ||
// TODO @ST-DDT 2024-02-25: Check why this is detected as `number` instead of `1` | ||
const actual = faker.helpers.maybe(() => 1); | ||
expectTypeOf(actual).toEqualTypeOf<number | undefined>(); | ||
}); | ||
}); | ||
|
||
describe('objectKey', () => { | ||
it('const generic single element', () => { | ||
const actual = faker.helpers.objectKey({ a: 1 }); | ||
expectTypeOf(actual).toEqualTypeOf<'a'>(); | ||
}); | ||
|
||
it('const generic multiple elements', () => { | ||
const actual = faker.helpers.objectKey({ a: 1, b: 'a', c: false }); | ||
expectTypeOf(actual).toEqualTypeOf<'a' | 'b' | 'c'>(); | ||
}); | ||
}); | ||
|
||
describe('objectValue', () => { | ||
it('const generic single element', () => { | ||
const actual = faker.helpers.objectValue({ a: 1 }); | ||
expectTypeOf(actual).toEqualTypeOf<1>(); | ||
}); | ||
|
||
it('const generic multiple elements', () => { | ||
const actual = faker.helpers.objectValue({ a: 1, b: 'a', c: false }); | ||
expectTypeOf(actual).toEqualTypeOf<1 | 'a' | false>(); | ||
}); | ||
}); | ||
|
||
describe('objectEntry', () => { | ||
it('const generic single element', () => { | ||
const actual = faker.helpers.objectEntry({ a: 1 }); | ||
expectTypeOf(actual).toEqualTypeOf<['a', 1]>(); | ||
}); | ||
|
||
it('const generic multiple elements', () => { | ||
const actual = faker.helpers.objectEntry({ a: 1, b: 'a', c: false }); | ||
// TODO @ST-DDT 2024-02-25: Check whether we can infer the return type any better | ||
expectTypeOf(actual).toEqualTypeOf<['a' | 'b' | 'c', false | 1 | 'a']>(); | ||
}); | ||
}); | ||
|
||
describe('arrayElement', () => { | ||
it('const generic single element', () => { | ||
const actual = faker.helpers.arrayElement([1]); | ||
expectTypeOf(actual).toEqualTypeOf<1>(); | ||
}); | ||
|
||
it('const generic multiple elements', () => { | ||
const actual = faker.helpers.arrayElement([1, 'a', false]); | ||
expectTypeOf(actual).toEqualTypeOf<1 | 'a' | false>(); | ||
}); | ||
}); | ||
|
||
describe('arrayElements', () => { | ||
it('const generic single element', () => { | ||
const actual = faker.helpers.arrayElements([1], 1); | ||
expectTypeOf(actual).toEqualTypeOf<Array<1>>(); | ||
}); | ||
|
||
it('const generic multiple elements', () => { | ||
const actual = faker.helpers.arrayElements([1, 'a', false], 3); | ||
expectTypeOf(actual).toEqualTypeOf<Array<1 | 'a' | false>>(); | ||
}); | ||
}); | ||
|
||
describe('multiple', () => { | ||
it('const generic single element', () => { | ||
const actual = faker.helpers.multiple(() => 1); | ||
expectTypeOf(actual).toEqualTypeOf<number[]>(); | ||
}); | ||
|
||
it('const generic multiple elements', () => { | ||
const actual = faker.helpers.multiple(() => 1, { count: 3 }); | ||
expectTypeOf(actual).toEqualTypeOf<number[]>(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters