Skip to content

Support optional properties in objects #38

Description

@thomasboyt

If I have the following type:

type MyType = {
  a: ?any;
  b: ?any;
}

And the following instance of it:

var myThing : MyType = {
  a: 'hello'
};

Flow will find the following error:

/Users/tboyt/Coding/mode7/src/Foo.js:2:15,5:1: property b
Property not found in
  /Users/tboyt/Coding/mode7/src/Foo.js:7:24,9:1: object literal

This makes sense, given that the maybe type is intended to be either the given type or null, not the type or undefined. However, not being able to define a property as entirely optional - e.g., being either a specific type or undefined - 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.

Activity

  1. avikchaudhuri commented on Nov 19, 2014

    @avikchaudhuri
    Contributor

    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.

  2. Raynos commented on Nov 19, 2014

    @Raynos
    Contributor

    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.

  3. gcanti commented on Jan 28, 2015

    @gcanti

    +1

  4. gcanti commented on Feb 28, 2015

    @gcanti

    As for v0.4 this issue seems fixed:

    /* @flow */
    
    var x: {a?: string;} = {}; // ok!
  5. samwgoldman commented on May 15, 2015

    @samwgoldman
    Member

    Can this be closed?

  6. samwgoldman commented on Jun 23, 2015

    @samwgoldman
    Member

    Closing. Please reopen if this is still an issue.

  7. romulof commented on Aug 9, 2016

    @romulof
    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 config to suppress this error message?

  8. jwebcat commented on Mar 24, 2017

    @jwebcat

    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

  9. matthewchung74 commented on Aug 31, 2017

    @matthewchung74

    I 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions