Subscribe on changes!

prop `boolean` required type error

avatar
Jul 27th 2023

Required:

<script lang="ts" setup>
const props = defineProps<{
  b1: boolean;
  b2: boolean;
  b3: boolean;
  s1: string;
  s2: string;
}>();
</script>

Optional:

<script lang="ts" setup>
const props = defineProps<{
  b1?: boolean;
  b2?: boolean;
  b3?: boolean;
  s1?: string;
  s2?: string;
}>();
</script>
avatar
Jul 28th 2023

PropType一般用于定义复杂变量(例如:数组、对象、函数)的类型定义,布尔类型或字符串类型直接定义即可,PropType会造成prop变量接口定义为可选变量。

avatar
Aug 11th 2023

also confuse here

Is the type returned by DefineProps generated by this Readonly<ExtractPropTypes<{ x:Boolean }>> ?

check the mini RP

https://www.typescriptlang.org/play?#code/C4TwDgpgBACgTgezAWQsAFggJgHgCoA0UeAwggHYDOwcArgMbAJxQC8UAhuSAHxtQBtPAF0oEAB7AI5LJUEAoKFAAUygHQaOcAOaUAXJ24BKNny4gTAHyi0ZEAGYBLchCzzRAeg9RH9n3PtbRkcKKAB3RwxOHUoiDgAbeIQI8m0ocgoAWjgIAEdaRxysKEDyYIpKRSgAfigAb3SIMJUjA1IKmgYmOABuFra+nI4sCniQKDBEJlBINrIqTsZmKABfKC8oEiGpKAAxIOAQ8ih4xwBraHoOuiW4KoMXADcIO-kJMGZgKBnoABEOYAcfgAJQgVzguGocGc2iItjOGTC5B48nkP1giDA8yhXWY+H45j4rCq1gaLma6k0MQM5gEwlaxCgADJ6msViT6v1GeylNZ4EhUBhsPgUWjwH8HBxaPFgLsOLcQCL+MpJkh9FB-oCTKw+HgoNZyNL4vqbHYnC43GLIBikHhxUr2PysddcXAlXzMdjFt0RXTUc4pHB7PLoE6APJgQ4VfHscxEX78PB8OpVH7VAxOu2Qd3fOjQA1Gqo5fKFVzpqAAIwQCHiEC4VSwkulwHLCesvybMrlCpwvz4BcSJtsjfNrhNCArACswcAqo8Eo4sADmNVlPP4rQIAZ4YjyAyqzW6+R5OyraHMfh44miTawBGo1RLxr+7esxARaj0fejgkANIQEBKA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CeCKPLIrVRyt7Rhg0Seb24gPF+02PVD4LhyDAEuowJ0lAHFARgPwTUHBEH+JEccAxSjtYwA0SaJiSTUQbH6uMDA0BxATExOkBAXxG60JqOAgAGiCChoxFRgRoNA2BQEGjiD0LvLgqxImoiAA

why the x prop is optional not required ?

avatar
Aug 11th 2023

also confuse here

Is the type returned by DefineProps generated by this Readonly<ExtractPropTypes<{ x:Boolean }>> ?

check the mini RP

https://www.typescriptlang.org/play?#code/C4TwDgpgBACgTgezAWQsAFggJgHgCoA0UeAwggHYDOwcArgMbAJxQC8UAhuSAHxtQBtPAF0oEAB7AI5LJUEAoKFAAUygHQaOcAOaUAXJ24BKNny4gTAHyi0ZEAGYBLchCzzRAeg9RH9n3PtbRkcKKAB3RwxOHUoiDgAbeIQI8m0ocgoAWjgIAEdaRxysKEDyYIpKRSgAfigAb3SIMJUjA1IKmgYmOABuFra+nI4sCniQKDBEJlBINrIqTsZmKABfKC8oEiGpKAAxIOAQ8ih4xwBraHoOuiW4KoMXADcIO-kJMGZgKBnoABEOYAcfgAJQgVzguGocGc2iItjOGTC5B48nkP1giDA8yhXWY+H45j4rCq1gaLma6k0MQM5gEwlaxCgADJ6msViT6v1GeylNZ4EhUBhsPgUWjwH8HBxaPFgLsOLcQCL+MpJkh9FB-oCTKw+HgoNZyNL4vqbHYnC43GLIBikHhxUr2PysddcXAlXzMdjFt0RXTUc4pHB7PLoE6APJgQ4VfHscxEX78PB8OpVH7VAxOu2Qd3fOjQA1Gqo5fKFVzpqAAIwQCHiEC4VSwkulwHLCesvybMrlCpwvz4BcSJtsjfNrhNCArACswcAqo8Eo4sADmNVlPP4rQIAZ4YjyAyqzW6+R5OyraHMfh44miTawBGo1RLxr+7esxARaj0fejgkANIQEBKAdKAAFFxHoDdGxwC4QAQPxCCgUESyKf9AJFUV0QASXIewXkze0k34IRRAkKQZDkARDUSYQqlqcx1m8KjjRZTpoDCBBpWKYNHGNJgfBwl57kEEQxEkaRZEEBofgeI0TVY1YaKUOju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LgqxImoiAA

why the x prop is optional not required ?

In my opinion, props in vue3 are defined as optional.