diff --git a/src/common/ipc/cluster.ipc.ts b/src/common/ipc/cluster.ipc.ts index b574e2a316..f1f416da86 100644 --- a/src/common/ipc/cluster.ipc.ts +++ b/src/common/ipc/cluster.ipc.ts @@ -3,7 +3,7 @@ import { ResourceApplier } from "../../main/resource-applier"; import { clusterFrameMap } from "../cluster-frames"; import { ClusterId, clusterStore } from "../cluster-store"; import { appEventBus } from "../event-bus"; -import { hasOptionalProperty, hasTypedProperty, isString, isBoolean, bindPredicate, isTypedArray } from "../utils/type-narrowing"; +import { hasOptionalProperty, hasTypedProperty, isString, isBoolean, bindTypeGuard, isTypedArray } from "../utils/type-narrowing"; import { createTypedInvoker, createTypedSender } from "./type-enforced-ipc"; export type ClusterIdArgList = [clusterId: ClusterId]; @@ -86,7 +86,7 @@ export const clusterKubectlApplyAll = createTypedInvoker({ }, verifier(args: unknown[]): args is [clusterId: ClusterId, resources: string[]] { return hasTypedProperty(args, 0, isString) - && hasTypedProperty(args, 1, bindPredicate(isTypedArray, isString)) + && hasTypedProperty(args, 1, bindTypeGuard(isTypedArray, isString)) && args.length === 2; }, }); diff --git a/src/common/ipc/update-available.ipc.ts b/src/common/ipc/update-available.ipc.ts index 29258455d0..e8aea9cf2a 100644 --- a/src/common/ipc/update-available.ipc.ts +++ b/src/common/ipc/update-available.ipc.ts @@ -1,6 +1,6 @@ import { UpdateInfo } from "electron-updater"; import { UpdateFileInfo, ReleaseNoteInfo } from "builder-util-runtime"; -import { bindPredicate, bindPredicateOr, hasOptionalProperty, hasTypedProperty, isNull, isObject, isString, isTypedArray, isNumber, isBoolean } from "../utils/type-narrowing"; +import { bindTypeGuard, createUnionGuard, hasOptionalProperty, hasTypedProperty, isNull, isObject, isString, isTypedArray, isNumber, isBoolean } from "../utils/type-narrowing"; import { createTypedSender } from "./type-enforced-ipc"; export const AutoUpdateLogPrefix = "[UPDATE-CHECKER]"; @@ -23,16 +23,16 @@ function isUpdateFileInfo(src: unknown): src is UpdateFileInfo { function isReleaseNoteInfo(src: unknown): src is ReleaseNoteInfo { return isObject(src) && hasTypedProperty(src, "version", isString) - && hasTypedProperty(src, "note", bindPredicateOr(isString, isNull)); + && hasTypedProperty(src, "note", createUnionGuard(isString, isNull)); } function isUpdateInfo(src: unknown): src is UpdateInfo { return isObject(src) && hasTypedProperty(src, "version", isString) && hasTypedProperty(src, "releaseDate", isString) - && hasTypedProperty(src, "files", bindPredicate(isTypedArray, isUpdateFileInfo)) - && hasOptionalProperty(src, "releaseName", bindPredicateOr(isString, isNull)) - && hasOptionalProperty(src, "releaseNotes", bindPredicateOr(isString, isReleaseNoteInfo, isNull)) + && hasTypedProperty(src, "files", bindTypeGuard(isTypedArray, isUpdateFileInfo)) + && hasOptionalProperty(src, "releaseName", createUnionGuard(isString, isNull)) + && hasOptionalProperty(src, "releaseNotes", createUnionGuard(isString, isReleaseNoteInfo, isNull)) && hasOptionalProperty(src, "stagingPercentage", isNumber); } diff --git a/src/common/utils/type-narrowing.ts b/src/common/utils/type-narrowing.ts index d4a6700d98..cdfa98666f 100644 --- a/src/common/utils/type-narrowing.ts +++ b/src/common/utils/type-narrowing.ts @@ -101,14 +101,14 @@ export function isNull(val: unknown): val is null { } /** - * Creates a new predicate function (with the same predicate) from `fn`. Such + * Creates a new type-guard function (with the same predicate) from `fn`. Such * that it can be called with just the value to be tested. * * This is useful for when using `hasOptionalProperty` and `hasTypedProperty` * @param fn A typescript user predicate function to be bound * @param boundArgs the set of arguments to be passed to `fn` in the new function */ -export function bindPredicate(fn: (arg1: unknown, ...args: FnArgs) => arg1 is T, ...boundArgs: FnArgs): (arg1: unknown) => arg1 is T { +export function bindTypeGuard(fn: (arg1: unknown, ...args: FnArgs) => arg1 is T, ...boundArgs: FnArgs): (arg1: unknown) => arg1 is T { return (arg1: unknown): arg1 is T => fn(arg1, ...boundArgs); } @@ -118,7 +118,12 @@ type First = T extends [infer R, ...any[]] ? R : any; type ReturnPredicateType src is any> = T extends (src: unknown) => src is infer R ? R : any; type OrReturnPredicateType[]> = ReturnPredicateType> | (T extends [any] ? never : OrReturnPredicateType>); -export function bindPredicateOr[]>(...predicates: Predicates): Predicate> { +/** + * Create a new type-guard for the union of the types that each of the + * predicates are type-guarding for + * @param predicates a list of predicates that should be executed in order + */ +export function createUnionGuard[]>(...predicates: Predicates): Predicate> { return (arg: unknown): arg is OrReturnPredicateType => { return predicates.some(predicate => predicate(arg)); };