Skip to content

Commit

Permalink
fix: simplify Omit
Browse files Browse the repository at this point in the history
  • Loading branch information
unional committed Mar 24, 2024
1 parent 42d2764 commit 7227250
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/itchy-mails-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"type-plus": patch
---

Simplify `Omit` type as the simpler code is working with typescript 5.4
12 changes: 9 additions & 3 deletions packages/type-plus/src/object/omit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,18 @@ describe('Omit<T, K>', () => {
expect(x.foo).toBe('bar')
})


test('intersection types with generic', () => {
type Foo = { a: string, b: string }
function foo<T>(input: Omit<Foo & T, 'a'>): void {
assertType.isString(input.b)
function foo<T>(input: Omit<Foo & T, 'b'>): void {
input.a = '1'
// @ts-expect-error Property 'b' does not exist
input.b = '1'

// @ts-expect-error Property 'c' does not exist.
input.c = '1'
}
foo({ b: '1' })
foo({ a: '1' })
})
})

Expand Down
8 changes: 3 additions & 5 deletions packages/type-plus/src/object/omit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ import { reduceByKey } from './reduceKey.js'
// by Titian Cernicova-Dragomir
// https://github.com/microsoft/TypeScript/issues/28339#issuecomment-463577347
// type-zoo
export type Omit<T, K extends UnionKeys<T>> = T extends T
? (keyof T extends K
? {}
: Pick<T, Exclude<keyof T, K>>)
: never
export type Omit<T, K extends UnionKeys<T>> = T extends unknown
? Pick<T, Exclude<keyof T, K>>
: never

/**
* @deprecated replaced by `Omit`
Expand Down

0 comments on commit 7227250

Please sign in to comment.