All files Key.ts

100% Statements 4/4
100% Branches 13/13
100% Functions 2/2
100% Lines 4/4

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    3x 18x               3x       5x                  
export type Key = string | number | symbol;
 
export function isKey(value: unknown): value is Key {
  return (
    value !== undefined &&
    (typeof value === 'string' ||
      (typeof value === 'number' && parseInt(value.toString())) === value ||
      typeof value === 'symbol')
  );
}
 
export function isKeyof<T extends object>(
  value: unknown,
  obj: T
): value is keyof T {
  return (
    isKey(value) &&
    ((typeof value === 'string' && Object.keys(obj).includes(value)) ||
      (typeof value === 'number' &&
        Object.keys(obj).includes(value.toString())) ||
      (typeof value === 'symbol' &&
        Object.getOwnPropertySymbols(obj).includes(value)))
  );
}