Repository navigation
fix(desktop): make BrowserWindow bindings typeable - #35907
Merged
Merged
Conversation
added 2 commits
July 10, 2026 13:36
`ValidBindings<T>` was a mapped type that discarded `T[K]`, replacing
every handler with a common supertype. That forced each handler's
parameters to be checked contravariantly against `BrowserWindowValue`,
so any handler narrowing a parameter — every useful handler — was
rejected. Passing an explicit type argument failed too, meaning `bind`
accepted nothing but a handler declared as `...args: BrowserWindowValue[]`.
Validate each handler and pass `T[K]` through unchanged instead, so
`bind` keeps the caller's parameter types and infers them from `T`.
`BrowserWindowReturn` was a union whose object arm was an index
signature, which rejected every user-declared `interface` — TypeScript
only grants implicit index signatures to type aliases. Replace it with a
structural walk that accepts interfaces (including recursive ones) and
rejects values that silently serialize to `{}`: `Date`, `Map`, `Set`,
class instances with methods, and functions. Returns are now checked
even when `T` is left to its default.
Also add `void` to the serializable leaves. It was absent while
`undefined` was present, so `async () => {}` never type checked, though
the runtime already coerces it via `result ?? null`.
Collapse `IsAny` + `IsUnknown` into a single `unknown extends T` guard, which holds for exactly those two types. Fold `BrowserWindowResolved` into the check that used it. Flatten the nested conditionals in `ValidBindings` by comparing the parameter and return types as one tuple. Rename `BrowserWindowReturn` to `BrowserWindowSerializable`, since it now validates arguments as well as return values. No behavior change: the same type-checking matrix passes and rejects.
crowlKats
force-pushed
the
fix/desktop-binding-types
branch
from
July 10, 2026 11:37
6d80a42 to
d3971c3
Compare
bartlomieju
approved these changes
Jul 20, 2026
bartlomieju
left a comment
Member
There was a problem hiding this comment.
Reviewed the type logic and verified it empirically by extracting the new types standalone and running them through a deno check matrix.
Confirmed:
- Parameter inference works under an explicit generic (
nameinfersstring,ninfersnumber), and narrowing handlers are no longer rejected. - Return checking fires even with the default
T:Date,Map, nestedDate, and function returns all fail, with the"binding handler must resolve with a serializable value"reason surfaced through the intersection. - Valid shapes pass: interfaces (recursive included), discriminated unions,
Record<string, unknown>,Uint8Array, andvoid(async () => {}now type checks, matching the runtimeresult ?? null). ValidBindings<T>rejects a non-serializable return baked into the bindings type at the type-argument position.- The removed
BrowserWindowReturnhas no references outside the replaced lines, so the deletion is clean.
Agree the missing type test over lib.deno.desktop.d.ts is worth adding as a follow-up since that gap is how the unusable constraint shipped, but not a blocker.
LGTM.
bartlomieju
added a commit
that referenced
this pull request
Jul 23, 2026
Closes #35837 --------- Co-authored-by: Leo Kettmeir <leo@deno.com> Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #35837.
The bug
win.bind("stat", async (path: string) => ...)does not type check, and neither does any other typed binding.ValidBindings<T>was a mapped type that discardedT[K]:So
T extends ValidBindings<T>required every handler to be assignable to(...args: BrowserWindowValue[]) => .... UnderstrictFunctionTypesthat check is contravariant in the parameters, so it demandedBrowserWindowValue(which includesnull) be assignable tostring. Any handler that narrows a parameter — i.e. every useful handler — was rejected.Supplying the generic did not help; it failed the same way. In practice
bindaccepted nothing except a handler literally declared as...args: BrowserWindowValue[].Separately,
BrowserWindowReturn's object arm was{ readonly [key: string]: unknown }. TypeScript only grants implicit index signatures to type aliases, never to interfaces, so that arm rejected every user-declaredinterface,Deno.FileInfoincluded.The fix
ValidBindings<T>now validates each handler and passesT[K]through unchanged, sobindkeeps the caller's own parameter types and infers them fromT.BrowserWindowReturn<T>becomes a structural walk instead of a union. It accepts interfaces, including recursive and mutually-recursive ones, and rejects the values that today silently serialize to{}—Date,Map,Set, class instances with methods, and functions.anyandunknownpass through, since nothing can be proven about them and rejecting them would breakRecord<string, unknown>payloads.Return values are now checked even when
Tis left to its default, so returning aDateis a type error rather than an empty object at runtime. The check sits in parameter position rather than as anR extends BrowserWindowReturn<R>constraint, because a type parameter constrained by a conditional type over itself is circular (TS2313).Also adds
voidto the serializable leaves. It was missing whileundefinedwas present, soasync () => {}— a fire-and-forget binding, the most common shape — never type checked, even though the runtime already coerces it (result ?? null).Note for the reporter
Deno.stat()still cannot be returned directly, but now it fails with a clear error instead of a confusing one.Deno.FileInfohasDatefields, andDatedoes not survive the boundary. Map it to a serializable shape first:Follow-up, not fixed here
Dateresolving to{}is a real runtime gap.BrowserWindowReturn's doc comment claims results are "serialized to JSON", butop_desktop_resolve_bind_calltakes#[serde] result: serde_json::Value, so values go through serde_v8, which has noDatesupport —deserialize_anymapsValueType::Objecttodeserialize_map, and aDatehas no own enumerable properties. Switching toJSON.stringifywould honortoJSON()but regressUint8Array, which currently arrives as a JSON array. Worth its own issue.Testing
Types only; no runtime change. Verified by type checking a matrix against the real
.d.tsbefore and after. Before: 4 errors, the first being the exactTS2345from the issue. After: 0.Covered as passing — no generic with narrow params; explicit generic in both method and property syntax; parameter inference from
T; interface, recursive interface, discriminated union,Record<string, unknown>,Uint8Array, tuple, andvoidreturns.Covered as still correctly rejected, via
@ts-expect-error(an unused directive is itself an error, so these are load-bearing) — wrong argument type; unknown binding name; handler return not matching the declared binding;Date, nestedDate,Map, function, and method-bearing returns;Dateparameter; non-async handler.There is no type test covering
lib.deno.desktop.d.ts— it is only referenced fromcli/build.rsandcli/tsc/mod.rs, which is how the constraint could ship unusable. A spec test that compiles a fewbindcalls would be worth adding; happy to do it here or as a follow-up.