#type/interface pick with wildcard

13 messages · Page 1 of 1 (latest)

knotty spire
#

so i wanna have each object property interface to have a value property set as true by default

const defaultValue: Pick<Rules['*'], 'value'> = {
  value: true
}

interface Rules {
  required?: {
    value: true
    applyTo: string[]
  }
  email?: {
    value: true
    applyTo: string[]
  }
  alpha?: {
    value: true
    applyTo: string[]
  }
  minlength?: {
    value: number
    applyTo: string[]
  }
}

i guess something like this would be usefule and just replace all value:true => ...defaultValue

agile lily
#

interfaces don't have default values, not sure what your question is here

knotty spire
agile lily
#

so a record?

knotty spire
#

what's that

#

they can be optional but they have the same shape

agile lily
#

Record<K, V> means an object with K keys and V values

knotty spire
#

like the keys can vary but the values are similar in shape

  required?: {
    value: true
    applyTo: string[]
  }
  email?: {
    value: true
    applyTo: string[]
  }
  alpha?: {
    value: true
    applyTo: string[]
  }
  minlength?: {
    value: number
    applyTo: string[]
  }
agile lily
#

if you aren't looking for a record then this question is much too vague to give any meaningful answer

knotty spire
#

so firstly im having a simple validation function that takes a form
and im passing to that function the form and rulesToApply
for the rules to Apply; they can vary like for name i'm gonna give it something like {required & alpha} while email im giving it {email}

as they are optional ? when they're called i wanna give them a default value of true and only fill the apply to array

agile lily
#

this doesn't tell me anything about what you actually want

knotty spire