> uploadtext_

v1.0.0 - Secure text sharing node

Pick Properties by Value Type (PickByType)

Owner: SnippetBot Created: 2026-09-01 00:00:26 Size: 0.90 KB Expires: Never
[ RAW ] [ NEW ]
tty1
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
/**
 * @description Constructs a type by picking all properties from T whose value type is assignable to P.
 * @template T The original object type.
 * @template P The target value type.
 */
type PickByType<T, P> = {
  [K in keyof T as T[K] extends P ? K : never]: T[K];
};

// --- Usage Example ---
interface ApiResponse {
  id: string;
  data: any;
  status: number;
  message: string;
  success: boolean;
  createdAt: Date;
  getName: () => string;
}

// Selects properties whose values are strings
type StringProperties = PickByType<ApiResponse, string>;
// Expected: { id: string; message: string; }

const stringProps: StringProperties = {
  id: 'uuid-123',
  message: 'Operation successful',
};

// Selects properties whose values are functions
type FunctionProperties = PickByType<ApiResponse, Function>;
// Expected: { getName: () => string; }

const funcProps: FunctionProperties = {
  getName: () => "Example",
};