1
  function resetForm () {
    // const INITIALFORM = {
    //   url: '',
    //   device: 'mobile',
    //   dimension: ['time', 'page'],
    // }
    for (const key in INITIALFORM) {
      type KeyType = keyof typeof INITIALFORM
      const initialValue = INITIALFORM[key as KeyType]
      //           ^ const initialValue: string | string[]
      form[key as KeyType] = initialValue
      /**
       * type KeyType = "url" | "device" | "dimension"
          不能将类型“string | string[]”分配给类型“string & string[]”。
            不能将类型“string”分配给类型“string & string[]”。
              不能将类型“string”分配给类型“string[]”
       */
    }
  }

i want to reset formData, ts throw error at form[key] = initialValue .

I desire the correct syntax override value

mihuar
  • 13
  • 3
  • Please see the [answer](https://stackoverflow.com/a/61377595/2887218) to the question this duplicates for more information. If I translate the code in that answer to this question, it yields [this result](https://tsplay.dev/w2K3zN). – jcalz Jul 30 '21 at 18:43

0 Answers0