Hone logo
Hone
Problems

Dynamically Omit Properties from TypeScript Objects

In many programming scenarios, you might need to create a new object based on an existing one, but with certain properties intentionally excluded. This is especially common when preparing data for APIs, logging, or when dealing with sensitive information that shouldn't be exposed. This challenge focuses on creating a robust TypeScript utility that allows you to dynamically omit specified properties from an object.

Problem Description

Your task is to create a TypeScript function named omit that takes two arguments:

  1. An object (obj).
  2. A string or an array of strings representing the keys to be omitted.

The function should return a new object that is a copy of the original object, but with the specified keys removed. The original object should remain unchanged.

Key Requirements:

  • The function must be generic to work with any object type.
  • It should accept a single key (string) or multiple keys (string array) to omit.
  • The returned object should have a type that reflects the omission of the specified keys.
  • The original object must not be mutated.

Expected Behavior:

If obj is { a: 1, b: 2, c: 3 } and you call omit(obj, 'b'), the output should be { a: 1, c: 3 }. If you call omit(obj, ['a', 'c']), the output should also be { a: 1, c: 3 }.

Edge Cases to Consider:

  • Omitting non-existent keys: If a key to be omitted does not exist in the original object, the function should simply ignore it and proceed.
  • Empty object as input: The function should handle an empty object gracefully, returning an empty object.
  • Omitting all keys: If all keys of an object are to be omitted, an empty object should be returned.

Examples

Example 1:

Input:
obj = { name: "Alice", age: 30, email: "alice@example.com" }
keysToOmit = "email"

Output:
{ name: "Alice", age: 30 }
Explanation: The 'email' property was omitted from the original object.

Example 2:

Input:
obj = { id: 101, title: "TypeScript Basics", completed: false }
keysToOmit = ["id", "completed"]

Output:
{ title: "TypeScript Basics" }
Explanation: Both 'id' and 'completed' properties were omitted.

Example 3:

Input:
obj = { a: 1, b: 2 }
keysToOmit = "c" // Key 'c' does not exist

Output:
{ a: 1, b: 2 }
Explanation: Omitting a non-existent key has no effect.

Example 4:

Input:
obj = {}
keysToOmit = "a"

Output:
{}
Explanation: An empty input object results in an empty output object.

Constraints

  • The input obj will always be a valid JavaScript object.
  • The keysToOmit argument will either be a single string or an array of strings.
  • The function should aim for reasonable performance; deep cloning is not required, a shallow copy is sufficient.

Notes

Consider how you can leverage TypeScript's utility types, particularly Omit and generics, to ensure type safety for both the input and the output. Think about how to handle the case where keysToOmit is a single string versus an array. You'll want to return a new object, so ensure you're not modifying the original object directly.

Loading editor...
typescript