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
do property not found in object literal errors really help anyone? #1606
Seriously, how does this help? I think this just wastes developer time. Tacking new properties onto objects, or deleting properties, are valid JS operations and don't contradict the declared return type here, so what gives?
JS is dynamically typed, the MongoDB API is dynamically typed, and therefore tacking new properties onto objects (even deleting properties sometimes) will always remain convenient, more convenient than Flow trying to help prevent whatever could hypothetically go wrong, in my opinion.
Reacted by Ivo de Kler, David Knoll, Jonathan del Strother, Sibelius Seraphini, Nick Olinger, David Warburton, Sir.Nathan (Jonathan Stassen), James Ide, Edoardo Colombo, Mark Matyas and 24 more
changed the title [-]property not found in object literal errors are an unhelpful PITA[/-][+]do property not found in object literal errors really help anyone?[/+]on Mar 31, 2016
let selector: Object={
...
}selector.hello='world'
I don't think the : Object should be necessary though.
Reacted by Olivier Tassinari, Adam Crabtree, Boris Shifrin, Sibelius Seraphini, Edoardo Colombo, ddzyndra, Charlie McGeorge, Jay C, spaceoi, Yan Takushevich and 30 more
Reacted by Mehdi Hosseinzadeh, Anton Kuznetsov, Saad Bin Saeed, Maxis Kao, Rob Gill, mahdi p, MarkNewcomb and Mountain/\Ash
Reacted by Sir.Nathan (Jonathan Stassen), Chris Domigan, Martin Zlámal, Scotty-efird, Willow Chargin, Rob Gill and hellopearl
Reacted by Alexander Dickson, Rob Gill and mahdi p
Creating a clone of the object or defining a new temporary variable both work so this isn't a show-stopper but it is a scenario where the programmer has to adopt to Flow instead of the other way around, which Flow is generally so much better at than other systems.
When you create an empty object it is "unsealed". This means that you can add any properties you want. This behaviour is not 100% safe, so one should be careful with it.
Reacted by Joe Grund, Hristo Oskov, Michael DePetrillo, matteocng, Turadg Aleahmad and Juriy Zaytsev
@vkurchatkin Okay, so in the case of my OP, the error is completely intentional and not going to change?
I was hoping that at least in this case since the added properties don't contradict the return type (which is all the object is being used for) that eventually flow could stop giving this error.
you can also avoid having to explicitly annotate the entire object by pre-declaring the optional properties as undefined, but the catch is that you have to annotate those undefineds to widen them because they won't be allowed to be widened later:
I ran into an unusual manifestation of this problem recently where the same syntax would pass in one file but fail in another.
// Works in file A.jsx:this._infoBoxStyle.display={[ActionStatus.FATAL]: 'block',[ActionStatus.WARNING]: 'block',[ActionStatus.RECOVERED]: 'none',}[this.props.appStatus];// Fails in file B.jsx:this._displayStatus={[ActionStatus.FATAL]: error,[ActionStatus.WARNING]: error,[ActionStatus.RECOVERED]: good,}[this._status]||good;// error:125: }[this._status]||good;^^^^^^^^^^^^property`Fatal`.Propertynotfoundin121: this._displayStatus={^objectliteral// The fix:this._displayStatus =({[ActionStatus.FATAL]: error,[ActionStatus.WARNING]: error,[ActionStatus.RECOVERED]: good,}: Object)[this._status]||good;// https://github.com/facebook/flow/issues/1606
I copied and pasted the statement from A into B and it fails in B as well. I then copied the statement from B into A and it passes in A!
Anyone know why it would pass in one file but not another? Both files were just defining react components, not doing anything differently that I can discern.
@colinhunt yes, but I think the issue might lie in the types of this._infoBoxStyle, this._displayStatus, this.props.appStatus, and this._status -- the syntax might be the same but there might be differences in the type defs for those variables. You might want to ask the same question with that additional info on Stack Overflow first. Or you're welcome to PM me with that.
IMO there really needs to be a configuration parameter for this, I'm using // $FlowFixMe whenever I add a field to an object literal.. When I want this behavior for an object, I use Object.seal().
We understand that this is not ideal coming from JavaScript, but this is the tradeoff we make in designing a type system for JavaScript. We will never be able to effectively type 100% of JavaScript as it would be way too expensive to think about every case and the type system may need to be looser and make less guarantees.
To fix the original issue, please refer to @vkurchatkin’s solutions which are what we recommend. If you have a specific case you would like to support please open a new issue.
I know this issue has been closed but in my opinion casting to generic Object is a poor fix for this that completely negates any advantages of flow. More often than not, even when assigning properties to an object, you want at least some properties guaranteed. In the original example, the OP listed beginTimestamp and endTimestamp as two such properties.
Rather than being all or nothing, I'd like to see a non-intrusive way to tell flow that other arbitrary properties can be set on this object, using spread operator without operand, for example:
interface FooBar {
foo: number;
bar: number;
}
interface FooBarBaz {
foo: number;
bar: number;
...
}
let object1: FooBar = { foo: 1, bar: 2};
let object2: FooBarBaz = { foo: 1, bar: 2};
object1.baz = 3; // error
object2.baz = 3; // no error
Activity
[-]property not found in object literal errors are an unhelpful PITA[/-][+]do property not found in object literal errors really help anyone?[/+]oliviertassinari commentedon Jul 23, 2016
Any idea how we can disable this type of warnings?
jedwards1211 commentedon Jul 23, 2016
@oliviertassinari yes, you can add
: Object:I don't think the
: Objectshould be necessary though.ide commentedon Dec 17, 2016
This is problematic when defining an object separate from its variable declaration:
https://flowtype.org/try/#0PTAEAEDMBsHsHcBQkCuA7AxgFwJazaABYCGaAJtAKYBOAFNZQI4A0oDAzgJSgDeiooKllCwARgCsAXKADyEytgDc-UDkih6TAHSjYZAJ7c+AgWPGgAvL1DFpARlABfZSdXrNjHXv1b4JLJQAbjRGKq5mOpagAEwuJo4qjqCU0OyUvGGgWITUCKBolPCgAKLUuXQA5AC2OOzsOGgA5mxMKJTswroGFZxxCQIqHFpp5LRmvYgJQA
Creating a clone of the object or defining a new temporary variable both work so this isn't a show-stopper but it is a scenario where the programmer has to adopt to Flow instead of the other way around, which Flow is generally so much better at than other systems.
jedwards1211 commentedon Dec 17, 2016
@oliviertassinari These days I often just cast to
Objectwhen tacking new properties on:jedwards1211 commentedon Dec 17, 2016
@ide you could do
(obj: Object).b = 2oliviertassinari commentedon Dec 17, 2016
@jedwards1211 Thanks for the tips.
vkurchatkin commentedon Dec 17, 2016
There are two options to deal with this problem:
We mark
bas optional so it can be added laterWhen you create an empty object it is "unsealed". This means that you can add any properties you want. This behaviour is not 100% safe, so one should be careful with it.
jedwards1211 commentedon Dec 17, 2016
@vkurchatkin Okay, so in the case of my OP, the error is completely intentional and not going to change?
I was hoping that at least in this case since the added properties don't contradict the return type (which is all the object is being used for) that eventually flow could stop giving this error.
vkurchatkin commentedon Dec 17, 2016
@jedwards1211 I agree, this behaviour is safe, pretty common, and ideally should be supported
mroch commentedon Dec 18, 2016
you can also avoid having to explicitly annotate the entire object by pre-declaring the optional properties as undefined, but the catch is that you have to annotate those
undefineds to widen them because they won't be allowed to be widened later:Example
Typecheck with Flow
Typecheck with Flow
Typecheck with Flow
Typecheck with Flow
colinhunt commentedon Jan 31, 2017
I ran into an unusual manifestation of this problem recently where the same syntax would pass in one file but fail in another.
I copied and pasted the statement from A into B and it fails in B as well. I then copied the statement from B into A and it passes in A!
Anyone know why it would pass in one file but not another? Both files were just defining react components, not doing anything differently that I can discern.
jedwards1211 commentedon Jan 31, 2017
@colinhunt I'm not sure that's related to my OP, because it doesn't involve adding new properties to objects created by object literals
colinhunt commentedon Jan 31, 2017
The fix suggested by @jedwards1211 fixes this though. That's why I added it here.
jedwards1211 commentedon Feb 1, 2017
@colinhunt yes, but that's just because
Objectis a very generic supertype, and casting toObjecteliminates all shape type errorscolinhunt commentedon Feb 1, 2017
Do you think I should create a new issue?
jedwards1211 commentedon Feb 1, 2017
@colinhunt yes, but I think the issue might lie in the types of
this._infoBoxStyle,this._displayStatus,this.props.appStatus, andthis._status-- the syntax might be the same but there might be differences in the type defs for those variables. You might want to ask the same question with that additional info on Stack Overflow first. Or you're welcome to PM me with that.memelet commentedon Mar 11, 2017
So when I add a function to a prototype I must always cast it down to Object?
nmn commentedon Mar 13, 2017
@memelet Yes that seems to be right.
cjdelisle commentedon Apr 2, 2017
IMO there really needs to be a configuration parameter for this, I'm using
// $FlowFixMewhenever I add a field to an object literal.. When I want this behavior for an object, I use Object.seal().matthewlamhk01 commentedon May 12, 2017
so if I suppose to add custom fields to some errors, creating a class for same kind for error objects is better than just use err: Object?
calebmer commentedon Jul 26, 2017
We understand that this is not ideal coming from JavaScript, but this is the tradeoff we make in designing a type system for JavaScript. We will never be able to effectively type 100% of JavaScript as it would be way too expensive to think about every case and the type system may need to be looser and make less guarantees.
To fix the original issue, please refer to @vkurchatkin’s solutions which are what we recommend. If you have a specific case you would like to support please open a new issue.
atsepkov commentedon Sep 26, 2019
I know this issue has been closed but in my opinion casting to generic
Objectis a poor fix for this that completely negates any advantages of flow. More often than not, even when assigning properties to an object, you want at least some properties guaranteed. In the original example, the OP listedbeginTimestampandendTimestampas two such properties.Rather than being all or nothing, I'd like to see a non-intrusive way to tell flow that other arbitrary properties can be set on this object, using spread operator without operand, for example: