Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | 35x 2156x | import { StringTypeValidator } from './../types'; /** * Make a validator function for a string-based type. * * This takes an array of allowed string values, and returns a function that * indicates whether the given value is one of the allowed values. This is * useful for when you have a prop whose type is a set of string constants, and * you need a validator for that prop. * * @example * import { ButtonAction } from './types'; * import { ButtonActions } from './constants'; * import { makeStringTypeValidator } from './utils/stringTypeValidator'; * const isButtonAction = makeStringTypeValidator( ButtonActions ); * * // in a component definition: * props: { * action: { * type: String as PropType<ButtonAction> * validator: isButtonAction * } * } * * // You can shorten this by inlining the isButtonAction variable, but * // then you have to explicitly specify the type to avoid errors: * props: { * action: { * type: String as PropType<ButtonAction> * validator: makeStringTypeValidator<ButtonAction>( ButtonActions ) * } * } * * @param allowedValues Allowed values * @return Function that takes a value and returns whether it is * one of allowedValues */ export function makeStringTypeValidator<T extends string>( allowedValues: readonly T[] ) : StringTypeValidator<T> { return ( s: unknown ) : s is T => typeof s === 'string' && ( allowedValues as readonly string[] ).indexOf( s ) !== -1; } |