if I have a type like:
type foo = {
bar?: ?string
}
eslint complains that i have no corresponding defaultProp declaration. However, if i set the default value to null:
static defaultProps = {
bar: null
}
I get a flow error when passing a string prop: this type is incompatible with null since flow infers the type from the defaultProp first (i believe). It seems like this rule shouldn't apply to maybe types...Thoughts?
What if you set the default to undefined?
@ljharb i tried that too i get the error this type is incompatible with undefined
To me that sounds like a flow bug - maybe types should be compatible with defaultProps. It'd be great if you filed a flow issue and linked it here.
@ljharb i can file an issue but i'm confused what value this validation provides on a Maybe type...if you initialize it to anything other than null or undefined then it isn't really a "maybe" type anyway since it will always have a value
Everything in js always has a value - when a maybe type is omitted, it'd be undefined, so at a minimum, that's what flow should allow. Equating undefined and null is also reasonable, if they decide to do it.
Created an issue in the flow project here facebook/flow#3982
I think it shouldn't require a default prop if the type is optional+maybe.
Think as implicit null/undefined.
@romulof defaultProps are important runtime information that's needed, and only having a flow type deprives the runtime of that info.
The official Flow docs never use maybe types for props and recommends using defaultProps if the prop should be optional. If you just set a prop's default value to null/undefined you will get your desired behavior.
I think this issue can be closed.
Most helpful comment
To me that sounds like a flow bug - maybe types should be compatible with defaultProps. It'd be great if you filed a flow issue and linked it here.