Effect CommunityEC
Effect Community2y ago
23 replies
Toby

Transforming a nullable string into a potentially undefined string

I’m trying to transform string | undefined | null => string | undefined

I tried this:

pipe(
  S.nullish(S.string),
  S.transform(
    S.optional(S.string), 
    s => s ?? undefined,
    identity
  )
)


But it chokes on S.optional which is not a Schema but a PropertySignature

So I tried a union

pipe(
  S.nullish(S.string),
  S.transform(
    S.union(S.string, S.undefined), 
    s => s ?? undefined,
    identity
  )
)


Which works, however any downstream errors result in a bloated stack:

├─ Union member
│  └─ Expected null, actual "https://example.com"
├─ Union member
│  └─ Expected undefined, actual "https://example.com"
└─ Union member
   └─ ...


Firstly, am I transforming the schemas correctly? and if so, is there a way to suppress the messaging about nulls and undefined?

Thanks
Was this page helpful?