Skip to content

Commit

Permalink
feat(types): add typescript support via index.d.ts file
Browse files Browse the repository at this point in the history
  • Loading branch information
justinwaite committed Jun 7, 2022
1 parent 08b1287 commit ab77250
Show file tree
Hide file tree
Showing 4 changed files with 696 additions and 3 deletions.
65 changes: 65 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import IntegerOptions = Chance.IntegerOptions;
import SentenceOptions = Chance.SentenceOptions;
import WordOptions = Chance.WordOptions;
import StringOptions = Chance.StringOptions;
import UrlOptions = Chance.UrlOptions;
import EmailOptions = Chance.EmailOptions;

declare function simpleObject(): Record<string, string>;

declare function objectWithKeys<T extends string, F, R = string>(
keys: T[],
options?: { factory?: F }
): Record<T, F extends (...args: any) => any ? ReturnType<F> : R>;

declare function fromList<T extends unknown>(list: T[]): T;

declare function subList<T extends unknown>(list: T[], options?: { size: number }): T[];

declare function listOf<F extends (...args: any) => any, T = ReturnType<F>>(
factory: F,
options?: { size?: number; uniqueOn?: string } & Parameters<typeof integer>[0]
): T[];

declare function string(options?: Partial<StringOptions>): string;

declare function word(options?: Partial<WordOptions>): string;

declare function sentence(options?: Partial<SentenceOptions>): string;

declare function paragraph(options?: { sentences?: number }): string;


declare function integer(options?: Partial<IntegerOptions>): number;

declare function float(options?: Partial<IntegerOptions> & { fixed?: number }): number;

declare function boolean(options?: { likelihood?: number }): boolean;

declare function url(options?: Partial<UrlOptions>): string;

declare function email(options?: Partial<EmailOptions>): string;

declare function date(): string;

declare interface Any {
simpleObject: typeof simpleObject,
objectWithKeys: typeof objectWithKeys,
fromList: typeof fromList,
subList: typeof subList,
listOf: typeof listOf,
string: typeof string,
word: typeof word,
sentence: typeof sentence,
paragraph: typeof paragraph,
integer: typeof integer,
float: typeof float,
boolean: typeof boolean,
url: typeof url,
email: typeof email,
date: typeof date,
}

declare const Any: Any;

export default Any;
58 changes: 58 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import {expectError, expectType} from 'tsd';
import any from '.';

expectType<{ [key: string]: string }>(any.simpleObject());

expectType<{ a: number, b: number, c: number }>(any.objectWithKeys(['a', 'b', 'c'], {factory: any.integer()}));
expectError(any.objectWithKeys())

expectType<string | number>(any.fromList(['a', 1]))
expectError(any.fromList())

expectType<string[]>(any.subList(['a', 'b', 'c']))
expectType<string[]>(any.subList(['a', 'b', 'c'], {size: 2}))
expectError(any.subList())

expectType<string>(any.string());
expectType<string>(any.string({length: 1}));
expectType<string>(any.string({length: 1, alpha: true, pool: 'abcde', casing: 'lower', numeric: true, symbols: true}));

expectType<string>(any.word())
expectType<string>(any.word({length: 1}))
expectType<string>(any.word({length: 1, capitalize: true}))
expectType<string>(any.word({length: 1, capitalize: true, syllables: 1}))

expectType<string>(any.sentence())
expectType<string>(any.sentence({words: 1}))
expectType<string>(any.sentence({words: 1, punctuation: '.'}))

expectType<string>(any.paragraph())
expectType<string>(any.paragraph({sentences: 1}))

expectType<number>(any.integer())
expectType<number>(any.integer({min: 1}))
expectType<number>(any.integer({min: 1, max: 2}))

expectType<number>(any.float())
expectType<number>(any.float({min: 1}))
expectType<number>(any.float({min: 1, max: 2}))
expectType<number>(any.float({min: 1, max: 2, fixed: 4}))

expectType<boolean>(any.boolean())
expectType<boolean>(any.boolean({likelihood: 30}))

expectType<string>(any.url());
expectType<string>(any.url({protocol: 'https'}));
expectType<string>(any.url({
protocol: 'https',
domain: 'google',
domain_prefix: 'a',
path: 'search',
extensions: ['.jpg']
}));

expectType<string>(any.email());
expectType<string>(any.email({length: 1}));
expectType<string>(any.email({length: 1, domain: 'gmail.com'}));

expectType<string>(any.date());
Loading

0 comments on commit ab77250

Please sign in to comment.