-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ability to define required nullable props #3948
Comments
You can use a category: {
required: true,
type: null as unknown as PropType<string | null>,
validator: (v: any) => typeof v === 'string' || v === null,
} I'm not sure this is the intended way. It's probably worth adding this to docs. I opened this PR https://github.com/vuejs/vue/pull/9358/files for Vue 2 but we didn't get anywhere with it. I personally would prefer being able to do |
On the other hand |
@posva Thank you a lot for the workaround 👍 I didn't know I can set Yet, having built-in support for that would of course be nice! Ok, I see the problem with Then what about the second variant: props: {
myProp: {
type: String,
required: true,
nullable: true,
}
} To me, it kinda makes sense:
Pros/cons for |
@posva saved my day! thanks mate! this issue went quite stale. any progress on that in sight ? |
Up, this issue is really annoying for the code readability and maintainability. We have two options for now :
|
bump |
Facing this problem as well, the database/json definitely have a lot of null, so I think ability to make a props type nullable is quite important
I use Quasar, they export another type definition to make |
The current work around is a bit silly but the only way to remove all the warnings etc. Would it be worthwhile having another PropType primitive like NullablePropType? such as |
ATM, we can try the solution below. It has type validation and absent checking. For const props = defineProps({
msg: {
type: [Boolean, null] as PropType<boolean | null>,
required: true
}
})
// or using type declaration
const props = defineProps<{
msg: boolean | null
}>()
props.msg
// ^ boolean | null TS: defineComponent({
props: {
msg: {
type: [Boolean, null] as PropType<boolean | null>,
required: true
}
},
setup(props) {
props.msg
// ^ boolean | null
}
}) |
How about this proposed syntax:
|
Somehow missed this when we landed bbf6ca9, which effectively implements proposed solution 1 (which is why the workaround suggested by @sxzz works). Added this detail to the docs in vuejs/docs@26666c0 For non-required props, they are already nullable in the first place. |
What problem does this feature solve?
I haven't found a way to properly define required nullable props.
Why
I want to be able to define a component property that is
required
and acceptsnull
as a value (along with "normal" types like String, Number, etc.)I consider
null
as a value, which indicates an absence of a value.undefined
means the value is unknown (e.g. we don't know it yet because we haven't got a response from API request).Hence,
null
andundefined
should not be treated the same.Problems with current workarounds
1. Marking the property as optional.
undefined
as a value, then I won't get any warnings from Vue.because TS considers
category
aspossibly 'undefined'
2. Mark the prop as required and hint TS about possible values
I get Vue warnings in the console "[Vue warn]: Invalid prop: type check failed for prop “category”. Expected String with value “null”, got Null"
What does the proposed API look like?
I have 2 ideas:
null
to the list of possibletype
values https://v3.vuejs.org/guide/component-props.html#type-checksSo that I can define the prop the following way:
nullable
option to the prop object (https://v3.vuejs.org/api/options-data.html#props):The text was updated successfully, but these errors were encountered: