Skip to content
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

"property not found in object literal" using dot notation and computed properties #1853

Closed
UnsungHero97 opened this issue May 26, 2016 · 7 comments

Comments

@UnsungHero97
Copy link

I'm getting property not found in object literal with the following code:

const API = {
  TYPE: 'foo'
};
API.someMethod = function() { // property 'someMethod' Property not found in object literal
  ...
};

However, if I change the code slightly, the error goes a way:

const API = {};
API.TYPE = 'foo';
API.someMethod = function() { // this is ok
  ...
};

What am I doing wrong in the first example? What is it that Flow is flagging as a problem?

@vkurchatkin
Copy link
Contributor

From docs:

When object types appear as annotations, they are considered sealed. Also, non-empty object literals are considered to have sealed object types. In fact, the only cases where an object type is not sealed are when it describes an empty object literal (to be extended by adding properties to it), an object literal with spread properties, or when it describes a map (see below).

@UnsungHero97
Copy link
Author

UnsungHero97 commented May 26, 2016

@vkurchatkin thank you, that makes sense. I think it'd be useful if the error message included something about the object literal being a sealed object.

@UnsungHero97
Copy link
Author

@vkurchatkin I ran into the same error for a slightly different use case, using computed properties:

const HTTP = {
  [HttpMethods.GET]: {},
  [HttpMethods.POST]: {},
  ...
};
HTTP.GET.doGet = function() {...} // property GET not found in object literal
HTTP.POST.doPost = function() {...} // property POST not found in object literal

Is this expected behavior? Is Flow correctly interpreting the object literal with computed properties?

@UnsungHero97 UnsungHero97 changed the title property not found in object literal using dot notation property not found in object literal using dot notation and computed properties Jun 2, 2016
@UnsungHero97 UnsungHero97 changed the title property not found in object literal using dot notation and computed properties "property not found in object literal" using dot notation and computed properties Jun 2, 2016
@UnsungHero97
Copy link
Author

@taoeffect
Copy link

OMFG. I struggled with this for a day.

Turns out that changing this:

export function makeResponse (
  event: EvType,
  data?: JSONType,
  err?: (string | {message: string})
) : Response {
  var response = {event}
  if (event === EVENT_TYPE.ERROR) {
    if (err) {
      response.data = data
      response.err = err
    } else {
      response.err = data
    }
  } else {
    response.data = data
  }
  return response
}

To this:

export function makeResponse (
  event: EvType,
  data?: JSONType,
  err?: (string | {message: string})
) : Response {
  var response: Response = {event} // <-- !!!! bs!!
  if (event === EVENT_TYPE.ERROR) {
    if (err) {
      response.data = data
      response.err = err
    } else {
      response.err = data
    }
  } else {
    response.data = data
  }
  return response
}

Made the nonsensical error go away. Of course it's a response type! It's what's being returned and that's clearly annotated in the function definition. Surprised Flow couldn't figure that out on its own and needed me to tell it.

@yantakus
Copy link

@vkurchatkin So what am I supposed to do in the following case:

const route = {
  path,
  name,
  getComponent () {
    // ...
  }
}
if (sagas) {
  route.onEnter = function () {
    // ...
  }
  route.onLeave = function () {
    // ...
  }
}

@yantakus
Copy link

Answering my own question. It could be:

const route: Object = { // ... }

More options in #1606

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

No branches or pull requests

5 participants