-
-
Notifications
You must be signed in to change notification settings - Fork 33.6k
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
Null is not treated as Object in component prop check #1961
Comments
It fails because Just initialize it to an empty object. |
Initializing with an {} will cause it to evaluate to true.. which means I won't be able to use |
@fnlctrl you just need to change it to |
@fnlctrl Which is a workaround, where either using such an exaggerated expression in template or writing a getter for each object to check if it is initialized seems not right. Using Typescript with Vue complicates this even more, as one has to denote all objects as What is the status of this issue in upcoming 3.0 release? |
Based on vuejs#1961. It wasn't possible to specify a required prop of type object and pass null as the value
When using Typescript, I have no way to specify My use-case is a filter that sets the value to null when cleared. Ideally, one of these would be the syntax: {type: [Null,String] as () => string | null}
{type: Any as () => string | null}
{type: [Object,String] as () => string | null} // with null considered an object Dead Ends// This will not work because 'Null' is not a type
{type: [Null,String] as () => string | null} // This will not work because 'Any' is not a type
{type: Any as () => string | null} // This will not work because null is not considered an Object, although is should be
{type: [Object,String] as () => string | null} // This will not work because string is not considered an Object
{type: Object as () => string | null} So I end up doing this, and the Typescript value is now undefined instead of {
validator: prop => typeof prop === 'string' || prop === null,
required: true,
} |
@Flamenco props: {
status: {
type: String as PropType<SomeType | null>,
default: null
}
} The typescript compiler understand |
Sadly this problem still exists. What's more annoying is the error saying. |
I think the key here is to use a default value of null: foo: {
type: (Object as () => Foo) || null,
default: null,
}, This works for me. If I use "required" instead of "default" I get the error mentioned above. |
Exactly as @dimitor115 and @mspoulsen said. |
I don't know why is this issue closed and have never been reopened since it has never been fixed. Setting a default value as null is not the same as explicitly requiring a value that could still be null. Forcing a default value and removing the It's a great issue when you need a dynamic prop that is updated often and could be set to null, like a selection component, which is a pretty common use case that has been mentioned by others and is also mine. |
I have a component that let's user select an object from an array.
and in parent template I have something like
which displays different content based on whether an object is selected.
and I get
I know i can just remove the "type: Object" check and suppress the warning,
but wouldn't it be better if component props checks treat null as an object?
Because javascript does that (typeof null === 'object'),
and setting null as an initial value (to my knowledge) doesn't affect mutation observation.
The text was updated successfully, but these errors were encountered: