Question about usage of Predicate.struct with optional properties

Hi I'm attempting to filter data using Predicate.struct but running into compilation issues regarding optional properties.

Given the following example, all is well:

declare const valueWithProp: { name: string | number };

[valueWithProp]
  .filter(Predicate.struct({ name: Predicate.isString })) // ok
  .map(result => result); // resulting type => { name: string }


But as soon as we add an optional property into the mix, I get a compilation error regarding name being undefined but required in the predicate type:

declare const valueWithOptionalProp: { name?: string | number };

[valueWithOptionalProp]
  .filter(Predicate.struct({ name: Predicate.isString })) // compile error
  .map(result => result); // resulting type => { name: string }


Is there a way to accomplish what I’m trying to do here? The example is a bit contrived, in reality I’m trying to apply it to a more complex struct.

Effect playground link for convenience:
https://effect.website/play#2d04c5bbafc9
Try Effect in your browser and share your code!
Effect Playground
Was this page helpful?