Unable to morph object values with default
TizzySaurus opened this issue ยท 2 comments
Report a bug
It seems it's currently impossible to morph and object value that has a default.
๐ Search Terms
object default, morph default
๐งฉ Context
- ArkType version: 2.0.0-beta.5
- TypeScript version (5.1+):
- Other context you think may be relevant (JS flavor, OS, etc.):
๐งโ๐ป Repro
const t = type({
foo: ["'on' | 'off' = 'on'", "=>", v => v === "on"]
});
The types indicate that this is valid, however at runtime it produces the following error:
Default values must be specified on objects like { isAdmin: 'boolean = false' }
It's not possible to use the syntax off foo: "'on'|'off' = 'on'"
since I want to morph the type. The following attempts also raise the same error:
const t1 = type({
foo: type("'on' | 'off' = 'on'").pipe(v => v === "on")
});
const t2 = type({
foo: type(["'on' | 'off' = 'on'", "=>", v => v === "on"])
});
Hmm, I think it occurred to me this wouldn't be possible, but I should handle it more clearly when someone tries to do it.
It doesn't really make sense to set a default value then apply a morph. You should just have the default value be whatever the output of your morph called on that default value would be and save yourself the extra computation.
I am considering removing the restriction on shallow default values/optional keys though which would make this stuff more flexible. For now though, I'm not going to address this individual case since it feels like an anti-pattern.