kakasoo / kakasoo/DeepStrictTypes

[FEAT] Add `deepStrictOmit` runtime function

Open
#52 0 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
TypeScript
Stars
64
Forks
2
PR merge metrics
No merged PRs in 30d

Description

# Feature Request

- [x] Extensions of existing features
- [ ] Propose a type that didn't exist before

**Type Expectation**

The runtime implementation for `deepStrictPick` exists, but its inverse operation `deepStrictOmit` has no runtime implementation. Since Pick and Omit are commonly used as a pair, a `deepStrictOmit` runtime function is needed for API completeness.

**Example Type**

```typescript
const input = {
a: 1,
b: { c: 2, d: 3 },
e: [{ f: 4, g: 5 }],
};

const result = deepStrictOmit(input, 'b.c');
// result: { a: 1, b: { d: 3 }, e: [{ f: 4, g: 5 }] }
// typeof result: DeepStrictOmit

const result2 = deepStrictOmit(input, 'e[*].f');
// result2: { a: 1, b: { c: 2, d: 3 }, e: [{ g: 5 }] }
```

**Proposed Solution**

1. Create `src/functions/DeepStrictOmit.ts`
2. Signature: `deepStrictOmit>(input: T, key: K): DeepStrictOmit`
3. Reference the traverse pattern from `deepStrictPick`, but invert the logic (only **remove** specified keys)
4. Add re-export to `src/functions/index.ts`
5. Use `[*]` pattern for removing keys from each element during array traversal

**Use Case**

- Remove sensitive fields from API responses (`password`, `secret`, etc.)
- Strip unnecessary nested fields before sending to client
- Provide a symmetric API alongside `deepStrictPick`

**Test Requirements**

All changes must include the following tests:

1. **Backward Compatibility**
- All existing tests must pass

2. **Feature Verification**
- Top-level key omit (`deepStrictOmit(obj, 'a')`)
- Nested key omit (`deepStrictOmit(obj, 'a.b')`)
- Array inner key omit (`deepStrictOmit(obj, 'items[*].id')`)
- Non-existent keys produce a type error (compile time)
- Return type matches `DeepStrictOmit`

3. **Complex Type Stability**
- 3+ levels of nested omit
- 2D array inner omit (`items[*].sub[*].value`)
- Omit from objects with Date properties (Date preserved)
- Omit from objects with branded types
- Omitting all keys results in an empty object
- Empty array input
- Immutability check (input is not mutated)

**How to verify:**
```bash
npm run build:test && npm run test
npm run prettier
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.