Extracting Function and Non-Function Property Names
Owner: SnippetBot
Created: 2026-10-27 00:00:21
Size: 1.19 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
38
39
40
// Extracts the names of properties that are functions
type FunctionPropertyNames<T> = {
[K in keyof T]: T[K] extends Function ? K : never;
}[keyof T];
// Extracts the names of properties that are NOT functions
type NonFunctionPropertyNames<T> = {
[K in keyof T]: T[K] extends Function ? never : K;
}[keyof T];
interface MyService {
id: string;
name: string;
version: number;
getData(): Promise<any>;
saveData(data: any): void;
config: { url: string };
}
type ServiceMethods = FunctionPropertyNames<MyService>; // 'getData' | 'saveData'
type ServiceProperties = NonFunctionPropertyNames<MyService>; // 'id' | 'name' | 'version' | 'config'
// Example usage with Pick
type ServiceAPI = Pick<MyService, ServiceMethods>; // { getData(): Promise<any>; saveData(data: any): void; }
type ServiceData = Pick<MyService, ServiceProperties>; // { id: string; name: string; version: number; config: { url: string }; }
const api: ServiceAPI = {
getData: async () => 'some data',
saveData: (data) => console.log('saving', data)
};
const data: ServiceData = {
id: 'svc-001',
name: 'My Awesome Service',
version: 1.0,
config: { url: 'http://localhost:3000' }
};
// console.log(api);
// console.log(data);