endojs / endo

Endo is a distributed secure JavaScript sandbox, based on SES
Apache License 2.0
761 stars 68 forks source link

Make `void` #2302

Closed michaelfig closed 1 month ago

michaelfig commented 1 month ago

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch @endo/pass-style@1.4.0 for the project I'm working on.

void is a TypeScript abstraction that reduces to a flavour of undefined when compiled to JS. It should be treated as a Primitive.

Without that, I get typing errors when trying to use PureData from @endo/pass-style:

    Types of property 'payload' are incompatible.
      Type 'VowPayload<string | number | bigint | boolean | symbol | void | CopyArrayI<never, never> | CopyRecordI<never, never> | CopyTaggedI<never, never> | null | undefined>' is not assignable to type 'VowPayload<PureData>'.

Here is the diff that solved my problem:

diff --git a/node_modules/@endo/pass-style/src/types.d.ts b/node_modules/@endo/pass-style/src/types.d.ts
index 6508067..ec59833 100644
--- a/node_modules/@endo/pass-style/src/types.d.ts
+++ b/node_modules/@endo/pass-style/src/types.d.ts
@@ -7,6 +7,7 @@ import { PASS_STYLE } from './passStyle-helpers.js';
 export type Primitive =
   | null
   | undefined
+  | void
   | string
   | number
   | boolean

This issue body was partially generated by patch-package.

erights commented 1 month ago

Hi @michaelfig , are you sure this is correctness preserving? I thought TS void was a don't care that could admit and ignore any value, not just primitives.

mhofman commented 1 month ago

void in a union type can have unintended consequences. I'm a little rusty on the exact problems, but I remember something about it erasing other types in the union in some cases (possibly only for return types)

mhofman commented 1 month ago

Here is a TS issue with more info: https://github.com/microsoft/TypeScript/issues/42709

michaelfig commented 1 month ago

Here is a TS issue

Thanks for the pointer. Indeed, I was holding things wrong, and corrected the problem by improving the typing of the functions I wrote that I wanted to be compatible with PureData.

Closing.