Skip to content

Commit 6f6c8f3

Browse files
authored
fix(types): better exact optional property types support (#2531)
fix: update optional properties to include undefined in type definitions across multiple packages
1 parent 075404e commit 6f6c8f3

File tree

51 files changed

+285
-181
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+285
-181
lines changed

.changeset/witty-cougars-wait.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
"@zag-js/interact-outside": patch
3+
"@zag-js/stringify-state": patch
4+
"@zag-js/highlight-word": patch
5+
"@zag-js/text-selection": patch
6+
"@zag-js/floating-panel": patch
7+
"@zag-js/focus-visible": patch
8+
"@zag-js/signature-pad": patch
9+
"@zag-js/element-rect": patch
10+
"@zag-js/element-size": patch
11+
"@zag-js/color-picker": patch
12+
"@zag-js/number-input": patch
13+
"@zag-js/rating-group": patch
14+
"@zag-js/toggle-group": patch
15+
"@zag-js/aria-hidden": patch
16+
"@zag-js/color-utils": patch
17+
"@zag-js/dismissable": patch
18+
"@zag-js/live-region": patch
19+
"@zag-js/collapsible": patch
20+
"@zag-js/date-picker": patch
21+
"@zag-js/file-upload": patch
22+
"@zag-js/radio-group": patch
23+
"@zag-js/time-picker": patch
24+
"@zag-js/collection": patch
25+
"@zag-js/file-utils": patch
26+
"@zag-js/form-utils": patch
27+
"@zag-js/i18n-utils": patch
28+
"@zag-js/hover-card": patch
29+
"@zag-js/pagination": patch
30+
"@zag-js/tags-input": patch
31+
"@zag-js/dom-event": patch
32+
"@zag-js/dom-query": patch
33+
"@zag-js/accordion": patch
34+
"@zag-js/clipboard": patch
35+
"@zag-js/pin-input": patch
36+
"@zag-js/tree-view": patch
37+
"@zag-js/preact": patch
38+
"@zag-js/svelte": patch
39+
"@zag-js/carousel": patch
40+
"@zag-js/checkbox": patch
41+
"@zag-js/combobox": patch
42+
"@zag-js/editable": patch
43+
"@zag-js/presence": patch
44+
"@zag-js/progress": patch
45+
"@zag-js/splitter": patch
46+
"@zag-js/react": patch
47+
"@zag-js/solid": patch
48+
"@zag-js/popover": patch
49+
"@zag-js/qr-code": patch
50+
"@zag-js/tooltip": patch
51+
"@zag-js/number-utils": patch
52+
"@zag-js/popper": patch
53+
"@zag-js/avatar": patch
54+
"@zag-js/dialog": patch
55+
"@zag-js/select": patch
56+
"@zag-js/slider": patch
57+
"@zag-js/switch": patch
58+
"@zag-js/vue": patch
59+
"@zag-js/steps": patch
60+
"@zag-js/timer": patch
61+
"@zag-js/toast": patch
62+
"@zag-js/utils": patch
63+
"@zag-js/rect-utils": patch
64+
"@zag-js/menu": patch
65+
"@zag-js/tabs": patch
66+
"@zag-js/tour": patch
67+
"@zag-js/store": patch
68+
"@zag-js/types": patch
69+
"@zag-js/core": patch
70+
"@zag-js/docs": patch
71+
---
72+
73+
Add better support for TS exactOptionalPropertyTypes setting.

packages/core/src/memo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export function memo<TDeps extends any[], TDepArgs, TResult>(
66
getDeps: (depArgs: TDepArgs) => [...TDeps],
77
fn: (...args: NoInfer<[...TDeps]>) => TResult,
88
opts?: {
9-
onChange?: (result: TResult) => void
9+
onChange?: ((result: TResult) => void) | undefined
1010
},
1111
): (depArgs: TDepArgs) => TResult {
1212
let deps: any[] = []

packages/core/src/types.ts

Lines changed: 71 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ type TrackFn = (deps: AnyFunction[], fn: VoidFunction) => void
3333
export interface BindableParams<T> {
3434
defaultValue?: T | undefined
3535
value?: T | undefined
36-
hash?: (a: T) => string
37-
isEqual?: (a: T, b: T | undefined) => boolean
38-
onChange?: (value: T, prev: T | undefined) => void
39-
debug?: string
40-
sync?: boolean
36+
hash?: ((a: T) => string) | undefined
37+
isEqual?: ((a: T, b: T | undefined) => boolean) | undefined
38+
onChange?: ((value: T, prev: T | undefined) => void) | undefined
39+
debug?: string | undefined
40+
sync?: boolean | undefined
4141
}
4242

4343
export type ValueOrFn<T> = T | ((prev: T) => T)
@@ -86,8 +86,8 @@ export interface Scope {
8686
}
8787

8888
type EventType<T = any> = T & {
89-
previousEvent?: T & { [key: string]: any }
90-
src?: string
89+
previousEvent?: (T & { [key: string]: any }) | undefined
90+
src?: string | undefined
9191
[key: string]: any
9292
}
9393

@@ -118,10 +118,10 @@ export interface Params<T extends Dict> {
118118
export type GuardFn<T extends Dict> = (params: Params<T>) => boolean
119119

120120
export interface Transition<T extends Dict> {
121-
target?: T["state"]
122-
actions?: T["action"][]
123-
guard?: T["guard"] | GuardFn<T>
124-
reenter?: boolean
121+
target?: T["state"] | undefined
122+
actions?: T["action"][] | undefined
123+
guard?: T["guard"] | GuardFn<T> | undefined
124+
reenter?: boolean | undefined
125125
}
126126

127127
type MaybeArray<T> = T | T[]
@@ -145,45 +145,61 @@ export type ActionsOrFn<T extends Dict> = T["action"][] | ((params: Params<T>) =
145145
export type EffectsOrFn<T extends Dict> = T["effect"][] | ((params: Params<T>) => T["effect"][] | undefined)
146146

147147
export interface Machine<T extends Dict> {
148-
debug?: boolean
149-
props?: (params: PropsParams<T>) => T["props"]
150-
context?: (params: ContextParams<T>) => {
151-
[K in keyof T["context"]]: Bindable<T["context"][K]>
152-
}
153-
computed?: {
154-
[K in keyof T["computed"]]: (params: ComputedParams<T>) => T["computed"][K]
155-
}
148+
debug?: boolean | undefined
149+
props?: ((params: PropsParams<T>) => T["props"]) | undefined
150+
context?:
151+
| ((params: ContextParams<T>) => {
152+
[K in keyof T["context"]]: Bindable<T["context"][K]>
153+
})
154+
| undefined
155+
computed?:
156+
| {
157+
[K in keyof T["computed"]]: (params: ComputedParams<T>) => T["computed"][K]
158+
}
159+
| undefined
156160
initialState: (params: { prop: PropFn<T> }) => T["state"]
157-
entry?: ActionsOrFn<T>
158-
exit?: ActionsOrFn<T>
159-
effects?: EffectsOrFn<T>
160-
refs?: (params: RefsParams<T>) => T["refs"]
161-
watch?: (params: Params<T>) => void
162-
on?: {
163-
[E in T["event"]["type"]]?: Transition<T> | Array<Transition<T>>
164-
}
165-
states: {
166-
[K in T["state"]]: {
167-
tags?: T["tag"][]
168-
entry?: ActionsOrFn<T>
169-
exit?: ActionsOrFn<T>
170-
effects?: EffectsOrFn<T>
171-
on?: {
161+
entry?: ActionsOrFn<T> | undefined
162+
exit?: ActionsOrFn<T> | undefined
163+
effects?: EffectsOrFn<T> | undefined
164+
refs?: ((params: RefsParams<T>) => T["refs"]) | undefined
165+
watch?: ((params: Params<T>) => void) | undefined
166+
on?:
167+
| {
172168
[E in T["event"]["type"]]?: Transition<T> | Array<Transition<T>>
173169
}
170+
| undefined
171+
states: {
172+
[K in T["state"]]: {
173+
tags?: T["tag"][] | undefined
174+
entry?: ActionsOrFn<T> | undefined
175+
exit?: ActionsOrFn<T> | undefined
176+
effects?: EffectsOrFn<T> | undefined
177+
on?:
178+
| {
179+
[E in T["event"]["type"]]?: Transition<T> | Array<Transition<T>>
180+
}
181+
| undefined
174182
}
175183
}
176-
implementations?: {
177-
guards?: {
178-
[K in T["guard"]]: (params: Params<T>) => boolean
179-
}
180-
actions?: {
181-
[K in T["action"]]: (params: Params<T>) => void
182-
}
183-
effects?: {
184-
[K in T["effect"]]: (params: Params<T>) => void | VoidFunction
185-
}
186-
}
184+
implementations?:
185+
| {
186+
guards?:
187+
| {
188+
[K in T["guard"]]: (params: Params<T>) => boolean
189+
}
190+
| undefined
191+
actions?:
192+
| {
193+
[K in T["action"]]: (params: Params<T>) => void
194+
}
195+
| undefined
196+
effects?:
197+
| {
198+
[K in T["effect"]]: (params: Params<T>) => void | VoidFunction
199+
}
200+
| undefined
201+
}
202+
| undefined
187203
}
188204

189205
interface MachineBaseProps {
@@ -194,16 +210,16 @@ interface MachineBaseProps {
194210
}
195211

196212
export interface MachineSchema {
197-
props?: MachineBaseProps
198-
context?: Record<string, any>
199-
refs?: Record<string, any>
200-
computed?: Record<string, any>
201-
state?: string
202-
tag?: string
203-
guard?: string
204-
action?: string
205-
effect?: string
206-
event?: { type: string } & Dict
213+
props?: MachineBaseProps | undefined
214+
context?: Record<string, any> | undefined
215+
refs?: Record<string, any> | undefined
216+
computed?: Record<string, any> | undefined
217+
state?: string | undefined
218+
tag?: string | undefined
219+
guard?: string | undefined
220+
action?: string | undefined
221+
effect?: string | undefined
222+
event?: ({ type: string } & Dict) | undefined
207223
}
208224

209225
type State<T extends MachineSchema> = Bindable<T["state"]> & {

packages/docs/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export function getAccessibilityDoc(key: AccessibilityDocKey): AccessibilityDoc
5050
interface Prop {
5151
type: string
5252
description: string
53-
defaultValue?: string
53+
defaultValue?: string | undefined
5454
}
5555

5656
export interface ApiDoc {

packages/frameworks/preact/src/portal.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import {
99
} from "preact/compat"
1010

1111
export interface PortalProps {
12-
disabled?: boolean
13-
container?: RefObject<HTMLElement>
14-
getRootNode?: () => ShadowRoot | Document | Node
12+
disabled?: boolean | undefined
13+
container?: RefObject<HTMLElement> | undefined
14+
getRootNode?: (() => ShadowRoot | Document | Node) | undefined
1515
}
1616

1717
export const Portal = (props: PropsWithChildren<PortalProps>): JSX.Element => {

packages/frameworks/react/src/portal.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import * as React from "react"
33
import { createPortal } from "react-dom"
44

55
export interface PortalProps {
6-
disabled?: boolean
7-
container?: RefObject<HTMLElement>
8-
getRootNode?: () => ShadowRoot | Document | Node
6+
disabled?: boolean | undefined
7+
container?: RefObject<HTMLElement> | undefined
8+
getRootNode?: (() => ShadowRoot | Document | Node) | undefined
99
}
1010

1111
export const Portal = (props: PropsWithChildren<PortalProps>): JSX.Element => {

packages/frameworks/svelte/src/normalize-props.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const propMap: Record<string, string> = {
1616

1717
export type PropTypes = SvelteHTMLElements & {
1818
element: HTMLAttributes<HTMLElement>
19-
style?: HTMLAttributes<HTMLElement>["style"]
19+
style?: HTMLAttributes<HTMLElement>["style"] | undefined
2020
}
2121

2222
export function toStyleString(style: Record<string, number | string>) {

packages/frameworks/svelte/src/portal.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export interface PortalActionProps {
2-
disabled?: boolean
3-
container?: HTMLElement
4-
getRootNode?: () => ShadowRoot | Document | Node
2+
disabled?: boolean | undefined
3+
container?: HTMLElement | undefined
4+
getRootNode?: (() => ShadowRoot | Document | Node) | undefined
55
}
66

77
export function portal(node: HTMLElement, props: PortalActionProps = {}) {

packages/frameworks/vue/src/normalize-props.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { createNormalizer } from "@zag-js/types"
22
import type * as Vue from "vue"
33

44
type ReservedProps = {
5-
key?: string | number | symbol
6-
ref?: Vue.VNodeRef
5+
key?: string | number | symbol | undefined
6+
ref?: Vue.VNodeRef | undefined
77
}
88

99
type Attrs<T> = T & ReservedProps

packages/machines/async-list/src/async-list.types.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export interface LoadDetails<C> {
1616

1717
export interface LoadResult<T, C> {
1818
items: T[]
19-
cursor?: C
19+
cursor?: C | undefined
2020
}
2121

2222
export interface SortDetails<T> {
@@ -34,7 +34,7 @@ export interface AsyncListProps<T, C> {
3434
/**
3535
* The function to call when the list is sorted
3636
*/
37-
sort?: (args: SortDetails<T>) => Promise<{ items: T[] }> | { items: T[] } | undefined
37+
sort?: ((args: SortDetails<T>) => Promise<{ items: T[] }> | { items: T[] } | undefined) | undefined
3838
/**
3939
* The initial items to display
4040
*/
@@ -58,11 +58,11 @@ export interface AsyncListProps<T, C> {
5858
/**
5959
* The function to call when the list is loaded successfully
6060
*/
61-
onSuccess?: (details: { items: T[] }) => void | undefined
61+
onSuccess?: ((details: { items: T[] }) => void | undefined) | undefined
6262
/**
6363
* The function to call when the list fails to load
6464
*/
65-
onError?: (details: { error: Error }) => void | undefined
65+
onError?: ((details: { error: Error }) => void | undefined) | undefined
6666
}
6767

6868
export interface AsyncListSchema<T, C> {
@@ -71,9 +71,9 @@ export interface AsyncListSchema<T, C> {
7171
context: {
7272
items: T[]
7373
filterText: string
74-
cursor?: C
75-
sortDescriptor?: SortDescriptor
76-
error?: any
74+
cursor?: C | undefined
75+
sortDescriptor?: SortDescriptor | undefined
76+
error?: any | undefined
7777
}
7878
refs: {
7979
abort: AbortController | null

0 commit comments

Comments
 (0)