annotations reset optional schema types to regular schemas
datner opened this issue ยท comments
๐ Bug report
Current Behavior
const foo = pipe(S.string, S.optional) // S.OptionalSchema<string, true>
const bar = pipe(S.string, S.optional, S.description('...')) // S.Schema<string>
Expected behavior
const foo = pipe(S.string, S.optional) // S.OptionalSchema<string, true>
const bar = pipe(S.string, S.optional, S.description('...')) // S.OptionalSchema<string, true>
Reproducible example
const foo = pipe(S.string, S.optional) // S.OptionalSchema<string, true>
const bar = pipe(S.string, S.optional, S.description('...')) // S.Schema<string>
Suggested solution(s)
add an overload for optional schema
Additional context
// this works, but yeah
const id = pipe(S.string, S.description('...'), S.optional)
Your environment
My personal computer โค๏ธ
Which versions of @fp-ts/schema
are affected by this issue? Did this work in previous versions of @fp-ts/schema
?
I didn't do regression checks, no idea
Software | Version(s) |
---|---|
@fp-ts/schema | 0.1.2 |
TypeScript | 4.9.4 |
add an overload for optional schema
This would be rather inconvenient, every schema combinator should take it into account.
That a field is optional is a volatile and local information that serves only to struct
for how its API is made, and optional
makes sense only used inside struct
, it does not have a broader meaning.
So I would try to do the opposite, that is, I would try to make the use of optional
even more local and restrictive by preventing an operation like this:
// this should raise a compiler error
pipe(S.string, S.optional, S.description('...'))
and forcing optional
to be used as a final wrapper of a Schema
pipe(S.string, S.description('...'), S.optional)