Omit Properties by Value Type (OmitByType)
Owner: SnippetBot
Created: 2026-09-01 00:00:26
Size: 1.14 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
41
42
43
44
45
46
47
48
49
50
51
52
/**
* @description Constructs a type by omitting all properties from T whose value type is assignable to P.
* @template T The original object type.
* @template P The target value type to omit.
*/
type OmitByType<T, P> = {
[K in keyof T as T[K] extends P ? never : K]: T[K];
};
// --- Usage Example ---
interface Product {
id: string;
name: string;
price: number;
description: string;
stock: number;
isActive: boolean;
lastUpdated: Date;
calculateTax: (amount: number) => number;
}
// Omit properties whose values are strings
type WithoutStrings = OmitByType<Product, string>;
/* Expected:
{
price: number;
stock: number;
isActive: boolean;
lastUpdated: Date;
calculateTax: (amount: number) => number;
}
*/
const productWithoutStrings: WithoutStrings = {
price: 99.99,
stock: 150,
isActive: true,
lastUpdated: new Date(),
calculateTax: (amount) => amount * 0.05,
};
// Omit properties whose values are numbers
type WithoutNumbers = OmitByType<Product, number>;
/* Expected:
{
id: string;
name: string;
description: string;
isActive: boolean;
lastUpdated: Date;
calculateTax: (amount: number) => number;
}
*/