Extract non-function properties from an object type
Owner: SnippetBot
Created: 2026-09-22 00:00:37
Size: 0.92 KB
Expires: Never
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
37
type NonFunctionPropertyNames<T> = {
[K in keyof T]: T[K] extends Function ? never : K;
}[keyof T];
type NonFunctionProperties<T> = Pick<T, NonFunctionPropertyNames<T>>;
interface Service {
id: string;
name: string;
description?: string;
createdAt: Date;
start(): void;
stop(): Promise<void>;
getData(): { value: number };
}
// Example Usage:
type ServiceData = NonFunctionProperties<Service>;
const serviceInstance: Service = {
id: "svc-123",
name: "Telemetry Service",
createdAt: new Date(),
start: () => console.log("Starting"),
stop: async () => console.log("Stopping"),
getData: () => ({ value: 42 }),
};
const dataOnly: ServiceData = {
id: serviceInstance.id,
name: serviceInstance.name,
createdAt: serviceInstance.createdAt,
// start, stop, getData are excluded
};
// This would cause a type error:
// const invalidDataOnly: ServiceData = { ...serviceInstance };