aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/image.ts3
-rw-r--r--src/image_providers/lorempixel.ts3
-rw-r--r--src/utils/types.ts17
3 files changed, 21 insertions, 2 deletions
diff --git a/src/image.ts b/src/image.ts
index 8aeab602..9fe7f603 100644
--- a/src/image.ts
+++ b/src/image.ts
@@ -2,6 +2,7 @@ import type { Faker } from '.';
import { LoremPicsum } from './image_providers/lorempicsum';
import { Lorempixel } from './image_providers/lorempixel';
import { Unsplash } from './image_providers/unsplash';
+import type { MethodsOf } from './utils/types';
/**
* Module to generate placeholder images.
@@ -40,7 +41,7 @@ export class Image {
* faker.image.image(1234, 2345, true) // 'http://loremflickr.com/1234/2345/nature?56789'
*/
image(width?: number, height?: number, randomize?: boolean): string {
- const categories = [
+ const categories: MethodsOf<Image, Image['image']> = [
'abstract',
'animals',
'business',
diff --git a/src/image_providers/lorempixel.ts b/src/image_providers/lorempixel.ts
index 914f1066..50e48ede 100644
--- a/src/image_providers/lorempixel.ts
+++ b/src/image_providers/lorempixel.ts
@@ -1,4 +1,5 @@
import type { Faker } from '..';
+import type { MethodsOf } from '../utils/types';
/**
* Module to generate links to random images on `https://lorempixel.com/`.
@@ -14,7 +15,7 @@ export class Lorempixel {
* @param randomize Whether to append a seed to the url. Defaults to `false`.
*/
image(width?: number, height?: number, randomize?: boolean): string {
- const categories = [
+ const categories: MethodsOf<Lorempixel, Lorempixel['image']> = [
'abstract',
'animals',
'business',
diff --git a/src/utils/types.ts b/src/utils/types.ts
new file mode 100644
index 00000000..705d02d1
--- /dev/null
+++ b/src/utils/types.ts
@@ -0,0 +1,17 @@
+/**
+ * Type that represents a single method/function name of the given type.
+ */
+export type MethodOf<
+ ObjectType,
+ Signature extends (...args) => unknown = (...args) => unknown
+> = {
+ [Key in keyof ObjectType]: ObjectType[Key] extends Signature ? Key : never;
+}[keyof ObjectType];
+
+/**
+ * Type that represents all method/function names of the given type.
+ */
+export type MethodsOf<
+ ObjectType,
+ Signature extends (...args) => unknown = (...args) => unknown
+> = ReadonlyArray<MethodOf<ObjectType, Signature>>;