Flow: Property not found in Class

Created on 13 Jul 2017  路  6Comments  路  Source: facebook/flow

My code is:

/* @flow */

export default class MyClass {
  constructor(myParam: string) {
    this.myParam = myParam
  }
}

Error being produced at https://flow.org/try/ is following:

5:     this.myParam = myParam
            ^ property `myParam`. Property not found in
5:     this.myParam = myParam
       ^ MyClass

Most helpful comment

@AlokBansal8 Flow forces you to specify types of your properties in class block like

/* @flow */

export default class MyClass {
  myParam: string;

  constructor(myParam: string) {
    this.myParam = myParam
  }
}

All 6 comments

@AlokBansal8 Flow forces you to specify types of your properties in class block like

/* @flow */

export default class MyClass {
  myParam: string;

  constructor(myParam: string) {
    this.myParam = myParam
  }
}

@TrySound Any thoughts on doing this if you're dynamically creating properties in your constructor...? I'm looping through an array to create a bunch of refs like so: (trying some fancy-pants animations where I need exact CSS properties and coordinates, so that's why I'm using so many refs)

import projects from '../data/projects'

...

constructor(props: Props) {
  super(props)
  Object.keys(projects).forEach((projectName) => {
    this[`${projectName}Ref`] = React.createRef()
  })
}

So when you do myParam: string there in the class block, do you know of a way to do that as dynamically in a loop somehow...? Thanks.

[key: string]: React.Ref<any>

Thanks, but that's giving me a parsing error. Maybe I'm using it wrong?

class WorkPane extends React.Component<Props> {
  [key: string]: React.Ref<any>

  constructor(props: Props) {
    super(props)
    Object.keys(projects).forEach((projectName) => {
      this[`${projectName}Ref`] = React.createRef()
    })
  }

Looks like my use case may be related to https://github.com/facebook/flow/issues/252

Exact same use case as you @elramus but getting this annoying error! :(

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  路  3Comments

pelotom picture pelotom  路  3Comments

bennoleslie picture bennoleslie  路  3Comments

funtaps picture funtaps  路  3Comments

philikon picture philikon  路  3Comments