Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R.anyPass() can function as a type guard #695

Merged
merged 1 commit into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions files/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ Notes:

*/
// @SINGLE_MARKER
export function anyPass<T, U extends T[]>(predicates: { [K in keyof U]: (x: T) => x is U[K]; }): (input: T) => input is U[number];
export function anyPass<T>(predicates: ((x: T) => boolean)[]): (input: T) => boolean;
export function anyPass<T>(predicates: ((...inputs: T[]) => boolean)[]): (...inputs: T[]) => boolean;

Expand Down
15 changes: 15 additions & 0 deletions source/anyPass-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,19 @@ describe('anyPass', () => {
const filtered2 = xs.filter(pred)
filtered2 // $ExpectType number[]
})
it('functions as a type guard', () => {
const isString = (x: unknown): x is string => typeof x === 'string';
const isNumber = (x: unknown): x is number => typeof x === 'number';
const isBoolean = (x: unknown): x is boolean => typeof x === 'boolean';

const isStringNumberOrBoolean = anyPass([isString, isNumber, isBoolean]);

isStringNumberOrBoolean // $ExpectType (input: unknown) => input is string | number | boolean

const aValue: unknown = 1;

if (isStringNumberOrBoolean(aValue)) {
aValue // $ExpectType string | number | boolean
}
})
})