Skip to content

Type-safe safeValue #58

Open
Open

Description

@nighca

safeValue is like $ of FieldState in formstate-x v2, while with some differences:

  1. Naming as safeValue (or something else) instead of $
  2. All states (including FormState, ArrayFormState, etc.) will provide safeValue
  3. It should be type-safe (deciding by validators - they may narrow type of value)

Activity

  1. nighca commented on Nov 23, 2021

    @nighca
    CollaboratorAuthor
  2. nighca commented on Nov 23, 2021

    @nighca
    CollaboratorAuthor

    Some ideas:

    1. Auto type inference by validators

    const state = new FieldState<string | null>('').validators(notNull) // safeValue: string

    It's hard, maybe impossible

    2. Generic type passing with validator

    const state = new FieldState<string | null>('').validators<string>(notNull) // safeValue: string

    3. Generic type passing with constructor

    const state = new FieldState<string | null, string>('').validators(notNull) // safeValue: string

    4. Type assertion while read safeValue

    const state = new FieldState<string | null>('').validators(notNull)
    const safeValue = state.safeValue! // string

    This is not real "type-safe"..

  3. self-assigned this
    on Apr 18, 2022
  4. mattleonowicz commented on May 11, 2022

    @mattleonowicz
    Contributor

    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.

  5. nighca commented on May 12, 2022

    @nighca
    CollaboratorAuthor

    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
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions