You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add support for destructuring parameters + default values #183
Flow is throwing an Unexpected token = error at a = 1. Interestingly, JSHint has an issue with this pattern too.
Reacted by Alon Burg, Kevin Smith, Clayton Watts, Edoardo Colombo, Bart Waardenburg, cesare soldini, Cristian Sima, Roman Frołow, Brendan Roche, choeller and 45 more
I have a question related to this issue: What's would be the expected behavior for typing params with default values?
My usage of default params tends to enforce strict types, e.g. this field is either 1 or some user-defined number, but it is always a number.
Considering the following:
/* @flow */typeparams={a: number}functionfn({ a =1}: params): params{return{ a }}fn({})
Would I get an error for not passing in an object that has a set to a number? It's optional, so I'm guessing it should probably be annotated as { a?: number } or { a: ?number }?
@cesarandreu It would be { a?: number }, which is the same as { a: void | number }. ?number is the same as void | null | number. null is not a real type annotation in Flow (edit: it is now), but the point remains.
@eyyub You have some work on this. I looked over your branch and I think it makes sense to try to land the parsing support first, then the type checking support separately. If you don't have bandwidth to do this, would you mind if I tried to put a bow on what you have so far?
@samwgoldman Np I agree ! And yes sorry I don't have 'a bandwith' (nice expression :p) to do this actually, so do as you want and do not hesitate to ask me if something is weird on my branch !
It's been more than two years since this issue was opened and it still doesn't work properly. What's the state of the issue? @samwgoldman did you improve on the partial fix?
Reacted by Eryk Napierała, Ahmed Tarek, Andrii Chernenko, weijarz, Laurin, Michael, DTX-92, Mike Lambert, Rafał Lindemann, recurrence and 24 more
We are running into the exact problem that @lukemartin describes. Would be great to get this fixed or at least have someone from the team comment on it.
Reacted by karelzoha, Jordan Wallet, Wynn Slater, nickluger and Luis Alfonso Higuera Gamboa
function foo(arg: {name: ?number}) {
const { name = 42 } = arg;
// the default value is only used for undefined values.
console.log((name: (number | null)));
}
Perhaps a separate issue, but I'm having a very similar problem with non-maybe types. I'm trying:
/* @flow */
type P = string | Array<string>;
function p(p: P = []) {}
type O = {p: P};
function o({p = []}: O) {}
And I get the following errors:
9: function o({p = []}: O) {}
^ array type [1] is incompatible with string [1].
References:
7: type O = {p: P};
^ [1]
9: function o({p = []}: O) {}
^ empty array literal [1] is incompatible with string [2].
References:
9: function o({p = []}: O) {}
^ [1]
7: type O = {p: P};
^ [2]
Interestingly, the errors go away if I simplify the type P to Array<string>. It's almost like Flow is expecting my default value to be all of the unioned types, rather than just one of them.
Activity
rauchg commentedon Jan 20, 2015
+1
cesarandreu commentedon Sep 21, 2015
I have a question related to this issue: What's would be the expected behavior for typing params with default values?
My usage of default params tends to enforce strict types, e.g. this field is either 1 or some user-defined number, but it is always a number.
Considering the following:
Would I get an error for not passing in an object that has
aset to a number? It's optional, so I'm guessing it should probably be annotated as{ a?: number }or{ a: ?number }?rauchg commentedon Sep 21, 2015
It could also infer the type from the default value? Making
a?: numberunnecessary to write (perhaps more obscure).samwgoldman commentedon Oct 3, 2015
@cesarandreu It would be
{ a?: number }, which is the same as{ a: void | number }.?numberis the same asvoid | null | number.nullis not a real type annotation in Flow (edit: it is now), but the point remains.cesarandreu commentedon Oct 3, 2015
Thanks!
samwgoldman commentedon Oct 13, 2015
@eyyub You have some work on this. I looked over your branch and I think it makes sense to try to land the parsing support first, then the type checking support separately. If you don't have bandwidth to do this, would you mind if I tried to put a bow on what you have so far?
eyyub commentedon Oct 13, 2015
@samwgoldman Np I agree ! And yes sorry I don't have 'a bandwith' (nice expression :p) to do this actually, so do as you want and do not hesitate to ask me if something is weird on my branch !
Arilas commentedon Dec 22, 2015
Any updates for this issue?
AlexGalays commentedon Jan 2, 2016
I need diz !
Having to refactor
const { a, b = false } = objinto
Just to please flow is very annoying :(
trobertsonsf commentedon Jan 8, 2016
Would love to get a fix for this. Lack of support for such a nice language feature makes it feel like I'm getting handcuffed by flow =/
31 remaining items
Jacse commentedon Jul 14, 2017
It's been more than two years since this issue was opened and it still doesn't work properly. What's the state of the issue? @samwgoldman did you improve on the partial fix?
pascalduez commentedon Oct 20, 2017
Hi,
wondering whether this is related to an issue I'm having with default rest parameters and union types:
https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVAXAngBwKZgAqAFngLZ4ByAhhWALxgDkAlgHZRxNgA+zAzgFcAxsLz9+3PkwTUATm3YBzKcwAm1NkrxymAbnTZ8RUnUYBvVGDAthcNgC4w-DHOUAaK2Dvw5Tl25angC+Bpi4BCTkeAAKcnA4DCbRvMkUNBRhRgRxCfxJltYYpngA-E5RFLk4IWFQgmzCGCz2YABicHAAFOZgxSmMrBxcYMFO1fwAlGCFYKjBQA
lukemartin commentedon Jan 18, 2018
Flow fails with this very simple example of specifying a maybe type with a default
nullvalue:https://flow.org/try/#0MYewdgzgLgBAZiEMC8MAUBvMBDAtgUxRjAFcAbMgXwC4s99qYB+aAJwEswBzSgShQB8MDACgAkKEggy+AHRkQXNDgK8A3CMpA
Rohit-L commentedon May 10, 2018
Is anybody from the flow team going to comment on this?
simlu commentedon Jun 20, 2018
We are running into the exact problem that @lukemartin describes. Would be great to get this fixed or at least have someone from the team comment on it.
sibelius commentedon Jul 18, 2018
what is still failing?
simlu commentedon Jul 18, 2018
@sibelius lukemartin has provided a good example. Do you have problems understanding it?
lukemartin commentedon Jul 19, 2018
hey @sibelius - this throws errors in 0.76.0:
https://flow.org/try/#0MYewdgzgLgBAZiEMC8MAUBvMBDAtgUxRjAFcAbMgXwC4s99qYB+aAJwEswBzSgShQB8MDACgAkKEggy+AHRkQXNDgK8A3CMpA
Naddiseo commentedon Aug 16, 2018
Using
$Subtypearound the annotation seems to work, but I'm not sure if that's the right way to go in all situations.https://flow.org/try/#0MYewdgzgLgBAZiEMC8MAUBvMBDAtgUxRjAFcAbMgXwC4YASAZRICMoBPAB3wB4s99aAfmgAnAJZgA5pQB8AShQyYGAFABIUJBBl8AOjIhJaHATkBuFZRVA
Buggytheclown commentedon Oct 19, 2018
As proposed in SO
simlu commentedon Oct 19, 2018
@Buggytheclown Not a big fan of changing code to accommodate flow. In our scenario (jetbrains) this would cause us to lose introspection as well.
Typecheck constants
tomscallon commentedon Feb 25, 2019
Perhaps a separate issue, but I'm having a very similar problem with non-maybe types. I'm trying:
And I get the following errors:
Interestingly, the errors go away if I simplify the type
PtoArray<string>. It's almost like Flow is expecting my default value to be all of the unioned types, rather than just one of them.Fix destructuring annots