Skip to content

Commit

Permalink
feat(validator): add isWhat util function, thanks to microsoft/Type…
Browse files Browse the repository at this point in the history
  • Loading branch information
NWYLZW committed Feb 22, 2024
1 parent 6569fcc commit 3ec0a7f
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/core/src/utils/isWhat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export const notMatched: unique symbol = Symbol('notMatched')

/**
* By using the built-in type guarding strategy, you can indirectly generate an `is` function without explicitly declaring
* the return type of the `is` function. This allows you to focus less on type declarations.
*
* @example
* ```ts
* const onlyStrings = ['a', 'b', 1, 2]
* .filter((x): x is string => typeof x === 'string')
* // Code like the one above can be handled using `isWhat` without explicitly declaring the return type
* const onlyStrings = ['a', 'b', 1, 2]
* .filter(isWhat(x => typeof x === 'number' ? x : notMatched))
* // The general pattern can be written as
* const onlyWhatType = ['a', 'b', 1, 2]
* .filter(isWhat(x => (${write type guard conditional expression}) ? x : notMatched))
* ```
*/
export function isWhat<T>(match: (x: unknown) => T | typeof notMatched): (x: unknown) => x is T {
return (x): x is T => match(x) !== notMatched
}

0 comments on commit 3ec0a7f

Please sign in to comment.