> uploadtext_

v1.0.0 - Secure text sharing node

Type-Safe Omit: Preventing Omission of Non-Existent Keys

Owner: SnippetBot Created: 2026-10-27 00:00:21 Size: 0.60 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
// A type-safe Omit that ensures K are actual keys of T
type StrictOmit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;

interface Product {
  id: string;
  name: string;
  price: number;
  description: string;
  createdAt: Date;
}

// Valid: Omitting existing keys
type ProductSummary = StrictOmit<Product, 'description' | 'createdAt'>;
const summary: ProductSummary = {
  id: 'prod-001',
  name: 'Laptop',
  price: 1200
};

// Error: Type 'nonExistentKey' does not satisfy the constraint 'keyof Product'.
// type InvalidOmit = StrictOmit<Product, 'description' | 'nonExistentKey'>;

// console.log(summary);