Repository navigation
Support optional properties in objects #38
Description
Activity
You can do b: null or b: undefined in the myThing literal to get by.
We may support optional property syntax in types, like so { a: ?any; b?: any } to let you omit properties, like you can omit arguments when parameters are declared optional. So that would typecheck your code as is.
There is a difference between optional properties and nullable properties.
{ b?: String }means if the b property exist then enable that feature with the string argument.{ b: String | null }means if the b property does not exist then default its value and use the feature.One is for optional features, the other is for default values.
+1
As for v0.4 this issue seems fixed:
/* @flow */ var x: {a?: string;} = {}; // ok!
Reacted by Evan Siroky and DimitryCan this be closed?
Closing. Please reopen if this is still an issue.
const config = { baseURL: apiUrl, }; if (accessToken) { // <-- property `headers`. Property not found in object literal config.headers = { Authorization: `Bearer ${accessToken}`, }; }
Do I have to manually add type hints to
configto suppress this error message?I know this is an old issue but just in case someone else is curious about the comment of @romulof you have to type the object like so:
const config : Object = { baseURL: "apiUrl", };
and no more error voila
Reacted by Rômulo Ruas and Cory DanielsonI know this is old, but am running into the case where I am checking a property that does not exist in a flow type.
var x: {a?: string;} = {}; // ok! if (x.b ==="something") { // do something }I think it would be helpful if this also gave an error.
If I have the following type:
And the following instance of it:
Flow will find the following error:
This makes sense, given that the maybe type is intended to be either the given type or
null, not the type orundefined. However, not being able to define a property as entirely optional - e.g., being either a specific type orundefined- seems like a big missing feature.For an example use case, imagine an API with a constructor that uses a hash of keyword arguments, most of which are optional. It would be great to be able to enforce the types of those arguments, if present, using Flow, and ignore missing arguments.