Skip to content

Commit

Permalink
fix: plain client method argument types for params with intersectio…
Browse files Browse the repository at this point in the history
…n types (#2503)
  • Loading branch information
andipaetzold authored Jan 9, 2025
1 parent 6fd5cac commit f32251e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
18 changes: 18 additions & 0 deletions lib/plain/wrappers/wrap.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,22 @@ describe('OptionalDefaults', () => {

expectTypeOf<Result>().toMatchTypeOf<Expected>()
})

it('handles intersection types', () => {
type Result = OptionalDefaults<{ foo1: 'bar1' } | { foo2: 'bar2' }>

type Expected = { foo1: 'bar1' } | { foo2: 'bar2' }

expectTypeOf<Result>().toMatchTypeOf<Expected>()
})

it('handles union with intersection types', () => {
type Result = OptionalDefaults<{ spaceId: string } & ({ foo1: 'bar1' } | { foo2: 'bar2' })>

type Expected =
| { spaceId?: string | undefined; foo1: 'bar1' }
| { spaceId?: string | undefined; foo2: 'bar2' }

expectTypeOf<Result>().toMatchTypeOf<Expected>()
})
})
6 changes: 5 additions & 1 deletion lib/plain/wrappers/wrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ export type DefaultParams = {
environmentId?: string
organizationId?: string
}
/**
* @private
*/
type UnionOmit<T, K extends PropertyKey> = T extends unknown ? Omit<T, K> : never

/**
* @private
*/
export type OptionalDefaults<T> = Omit<T, keyof DefaultParams> &
export type OptionalDefaults<T> = UnionOmit<T, keyof DefaultParams> &
Partial<Pick<T, Extract<keyof T, keyof DefaultParams>>>

/**
Expand Down

0 comments on commit f32251e

Please sign in to comment.