-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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 support for destructuring parameters + default values #183
Comments
+1 |
Thank you for reporting this issue and appreciate your patience. We've notified the core team for an update on this issue. We're looking for a response within the next 30 days or the issue may be closed. |
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 */
type params = { a: number }
function fn ({ a = 1 }: params): params {
return { a }
}
fn({}) Would I get an error for not passing in an object that has |
It could also infer the type from the default value? Making |
@cesarandreu It would be |
Thanks! |
@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 ! |
Any updates for this issue? |
I need diz ! Having to refactor
into
Just to please flow is very annoying :( |
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 =/ |
[Accidentally placed this comment in the wrong thread. Putting it here for posterity.] I had a fair amount of WIP on this, but I tossed it out and haven't revisited. My take is that this commit is too hacky and we shouldn't merge it. We need to separate parsing of objects and parsing of destructuring. Right now we do this weird thing where we parse an object, then transform it. Object syntax doesn't have defaults, so that method no longer works. Ultimately this just confuses parsing support for assignment expressions ( I can still loop back on this in the coming weeks, but if someone is looking for something to do, please take it. If you do, I heartily recommend starting with the params/declaration cases and punting on assignment exprs. That's just me, though. |
This is currently the biggest missing es2015 feature. Not being able to write the full es2015 spec makes it difficult to consider moving to flowtype. Therefore I hope this is a high priority missing feature. |
Considering ES2015 parameter syntax is already implemented by all 3 major browsers, this is a sever limitation of Flow. |
Yup, this has been on my plate for too long. I've been blocking this because I started working on it, then joined Facebook where I'm still getting used to things. Thanks for your patience. This feature is really high on my list of things to get out the door. |
This commit uses ES2015 destructuring assignment to swap two variables instead of using a tmp var. Flow has an open issue (facebook/flow#183) with this feature, though, so // $FlowFixMe is used to temporarily suppress the error.
Flow fails to parse this valid ES2015 code: var href = 'https://github.com/facebook/flow/issues/183'
var HREF_REGEXP = /^([^#?]*)(\?([^#]*))?(#(.+))?$/;
var [, path='', , query='', , hash=''] = HREF_REGEXP.exec(href) || [];
// ^ Unexpected token , The parsing error is suppressed if we put some dummy variables in the holes or if we remove the default assignments. |
Shouldn't types from destructured params with default values be inferred from the default value as mentioned in @rauchg comment? It seems really redundant and verbose to do:
seems like this should be enough:
Sorry if there is already support for this already by my flow linter is lighting up |
Same as reported by @alexfedoseev, but with optional type: http://bit.ly/2rYnuYg
|
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? |
Hi, wondering whether this is related to an issue I'm having with default rest parameters and union types: |
Flow fails with this very simple example of specifying a maybe type with a default
|
Is anybody from the flow team going to comment on this? |
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. |
what is still failing? |
@sibelius lukemartin has provided a good example. Do you have problems understanding it? |
hey @sibelius - this throws errors in 0.76.0:
|
Using
|
As proposed in SO
|
@Buggytheclown Not a big fan of changing code to accommodate flow. In our scenario (jetbrains) this would cause us to lose introspection as well. |
Flow mistakenly infers Maybe types when object destructuring is used with default values (#183). As a workaround, we use just and fromMaybe from utils. facebook/flow#183 Note that dotenv-webpack requires us to reference process.env properties explicitly. The following will not work: export const API_URL = env('API_URL')
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 |
Summary: Bindings introduced by destructuring a type annotation should behave as annotations themselves. That is, `p` in `var {p}: {p: T}` should constrain any subsequent write to be a subtype of `T`. This logic works today by wrapping the binding in an AnnotT using BecomeT internally, which works for most cases. However, since BecomeT performs unification, it is necessary that its lower bound be resolve once and exactly once. The once-and-exactly-once invariant is broken by union-like types when combined with the destructuring operation. Consider the following example: ``` var {p}: {p:string}|{p:number} = ... ``` When evaluating this variable declaration, we emit the following constraints: 1. {p:string}|{p:number} -> GetPropT ('p', T) 2. T -> BecomeT T' 3. P = T' Constraint (1) is processed by splitting the lower bound, which turns into two separate constraints `string -> T` and `number -> T`. Since `T` resolves twice, the `BecomeT` constraint misbehaves and we see a spurious error that `number` is not compatible with `string`. This diff deals with the above by handling union-like types specially in the `DestructuringT` use type. Fixes #183 Fixes #2198 Fixes #2220 Fixes #4077 Fixes #4270 Fixes #5461 Fixes #5745 Fixes #6408 Reviewed By: panagosg7 Differential Revision: D15457398 fbshipit-source-id: 22c9aba1e1df475c73b36a92bdf7ff5cf57504a6
I'm using ES6's new destructuring parameters with default values per the reminder from Brendan Eich (via 2ality.com).
So I would like to do this:
Flow is throwing an
Unexpected token =
error ata = 1
. Interestingly, JSHint has an issue with this pattern too.The text was updated successfully, but these errors were encountered: