Skip to content

Commit 73da25c

Browse files
committed
types: use common types instead of repeated types
1 parent 231edb8 commit 73da25c

File tree

17 files changed

+84
-65
lines changed

17 files changed

+84
-65
lines changed

packages/n4s/src/runtime/genEnforceLazy.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
CB,
55
Stringable,
66
Maybe,
7+
DynamicValue,
78
} from 'vest-utils';
89

910
import { ctx } from 'enforceContext';
@@ -91,6 +92,7 @@ export type LazyRuleRunners = {
9192
export type ComposeResult = LazyRuleRunners & ((value: any) => void);
9293

9394
type RegisteredRules = Array<(value: RuleValue) => RuleDetailedResult>;
94-
type LazyMessage =
95-
| string
96-
| ((value: unknown, originalMessage?: Stringable) => string);
95+
type LazyMessage = DynamicValue<
96+
string,
97+
[value: unknown, originalMessage?: Stringable]
98+
>;

packages/vast/src/vast.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { Maybe, isFunction, optionalFunctionValue } from 'vest-utils';
1+
import {
2+
DynamicValue,
3+
Maybe,
4+
isFunction,
5+
optionalFunctionValue,
6+
} from 'vest-utils';
27

38
// eslint-disable-next-line max-lines-per-function
49
export function createState(
@@ -87,8 +92,8 @@ export function createState(
8792
}
8893
}
8994

90-
type StateInput<S> = S | ((prevState?: S) => S);
91-
type SetStateInput<S> = S | ((prevState: S) => S);
95+
type StateInput<S> = DynamicValue<S, [prevState?: S]>;
96+
type SetStateInput<S> = DynamicValue<S, [prevState: S]>;
9297

9398
export type State = CreateStateReturn;
9499
export type StateHandlerReturn<S> = [S, (nextState: SetStateInput<S>) => void];
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import optionalFunctionValue from 'optionalFunctionValue';
2-
import { Nullish } from 'utilityTypes';
2+
import { DynamicValue, Nullish } from 'utilityTypes';
33

44
export default function defaultTo<T>(
5-
value: Nullish<T> | ((...args: any[]) => Nullish<T>),
6-
defaultValue: T | (() => T)
5+
value: DynamicValue<Nullish<T>>,
6+
defaultValue: DynamicValue<T>
77
): T {
88
return optionalFunctionValue(value) ?? optionalFunctionValue(defaultValue);
99
}

packages/vest-utils/src/optionalFunctionValue.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import isFunction from 'isFunction';
2+
import { DynamicValue } from 'utilityTypes';
23

34
export default function optionalFunctionValue<T>(
4-
value: T | ((...args: any[]) => T),
5+
value: DynamicValue<T>,
56
...args: unknown[]
67
): T {
78
return isFunction(value) ? value(...args) : value;
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import optionalFunctionValue from 'optionalFunctionValue';
2+
import { DynamicValue } from 'utilityTypes';
23

3-
export function createTinyState<S>(initialValue: S | (() => S)): TinyState<S> {
4+
export function createTinyState<S>(
5+
initialValue: SetValueInput<S>
6+
): TinyState<S> {
47
let value: S;
58

69
resetValue();
710

811
return () => [value, setValue, resetValue];
912

10-
function setValue(nextValue: S | ((currentValue: S) => S)) {
13+
function setValue(nextValue: SetValueInput<S>) {
1114
value = optionalFunctionValue(nextValue, value);
1215
}
1316

@@ -18,6 +21,8 @@ export function createTinyState<S>(initialValue: S | (() => S)): TinyState<S> {
1821

1922
export type TinyState<S> = () => [
2023
value: S,
21-
setValue: (next: S | ((prev: S) => S)) => void,
24+
setValue: (next: SetValueInput<S>) => void,
2225
resetValue: () => void
2326
];
27+
28+
type SetValueInput<S> = DynamicValue<S, [prev: S]>;

packages/vest-utils/src/utilityTypes.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ export type DropFirst<T extends unknown[]> = T extends [unknown, ...infer U]
22
? U
33
: never;
44

5-
export type Stringable = string | ((...args: any[]) => string);
5+
export type Stringable = string | CB<string>;
66

7-
export type CB = (...args: any[]) => any;
7+
export type CB<T = any, Args extends TArgs = TArgs> = (...args: Args) => T;
88

99
export type ValueOf<T> = T[keyof T];
1010

@@ -13,3 +13,9 @@ export type Nullish<T = void> = Nullable<T> | Maybe<T>;
1313
export type Nullable<T> = T | null;
1414

1515
export type Maybe<T> = T | undefined;
16+
17+
export type OneOrMoreOf<T> = T | T[];
18+
19+
export type DynamicValue<T, Args extends TArgs = TArgs> = T | CB<T, Args>;
20+
21+
type TArgs = any[];

packages/vest-utils/src/vest-utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,6 @@ export type {
4444
Nullish,
4545
Nullable,
4646
Maybe,
47+
OneOrMoreOf,
48+
DynamicValue,
4749
} from 'utilityTypes';

packages/vest/src/core/context/SuiteContext.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createCascade } from 'context';
22
import { assign, TinyState, tinyState, cache, CacheApi } from 'vest-utils';
3+
import { DynamicValue } from 'vest-utils/src/utilityTypes';
34

45
import { IsolateTest } from 'IsolateTest';
56
import { Modes } from 'Modes';
@@ -25,7 +26,7 @@ export const SuiteContext = createCascade<CTXType>((ctxRef, parentContext) => {
2526

2627
type CTXType = {
2728
exclusion: TExclusion;
28-
inclusion: Record<string, boolean | (() => boolean)>;
29+
inclusion: Record<string, DynamicValue<boolean>>;
2930
currentTest?: IsolateTest;
3031
groupName?: string;
3132
skipped?: boolean;

packages/vest/src/exports/parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import { suiteSelectors } from 'vest';
12
import { hasOwnProperty, invariant, isNullish, isPositive } from 'vest-utils';
23

34
import { ErrorStrings } from 'ErrorStrings';
45
import { SuiteSummary, TFieldName, TGroupName } from 'SuiteResultTypes';
5-
import { suiteSelectors } from 'vest';
66

77
export function parse<F extends TFieldName, G extends TGroupName>(
88
summary: SuiteSummary<F, G>

packages/vest/src/hooks/exclusive.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
hasOwnProperty,
55
optionalFunctionValue,
66
Maybe,
7+
OneOrMoreOf,
78
} from 'vest-utils';
89

910
import { ErrorStrings } from 'ErrorStrings';
@@ -12,9 +13,9 @@ import { TExclusion, useExclusion, useInclusion } from 'SuiteContext';
1213
import { TFieldName, TGroupName } from 'SuiteResultTypes';
1314
import { useIsExcludedIndividually } from 'skipWhen';
1415

15-
export type ExclusionItem = Maybe<string | string[]>;
16-
export type FieldExclusion<F extends TFieldName> = Maybe<F | F[]>;
17-
export type GroupExclusion<G extends TGroupName> = Maybe<G | G[]>;
16+
export type ExclusionItem = Maybe<OneOrMoreOf<string>>;
17+
export type FieldExclusion<F extends TFieldName> = Maybe<OneOrMoreOf<F>>;
18+
export type GroupExclusion<G extends TGroupName> = Maybe<OneOrMoreOf<G>>;
1819

1920
/**
2021
* Adds a field or a list of fields into the inclusion list

0 commit comments

Comments
 (0)