Open nighca opened 3 years ago
Related discussion: https://github.com/qbox/horn/pull/340#discussion_r699824181
Some ideas:
const state = new FieldState<string | null>('').validators(notNull) // safeValue: string
It's hard, maybe impossible
const state = new FieldState<string | null>('').validators<string>(notNull) // safeValue: string
const state = new FieldState<string | null, string>('').validators(notNull) // safeValue: string
safeValue
const state = new FieldState<string | null>('').validators(notNull)
const safeValue = state.safeValue! // string
This is not real "type-safe"..
One thing to consider: should this value be defined and available even when validators return errors? If not, then 2. makes sense.
But if this value is always defined, regardless of latest validation result, then I would suggest not linking the safeType with validators as in 2.
One thing to consider: should this value be defined and available even when validators return errors?
Accessing safe-value will throw if currently validation not passed.
@computed get safeValue() {
if (!this.validated || this.hasError) throw new Error('TODO')
return this.value
}
safeValue
is like$
ofFieldState
in formstate-x v2, while with some differences:safeValue
(or something else) instead of$
FormState
,ArrayFormState
, etc.) will providesafeValue