Skip to content

Declaring an unitialized class property. #6811

@nicolo-ribaudo

Description

@nicolo-ribaudo

Suppose I have an existing project to which I am adding flow types:

// Original project:

class Foo {
  x = 2;
  y = 3;
}

class Bar extends Foo {
  x;

  sum() {
    const sum = (this.x || 5) + this.y;
    console.log(sum);
  }
}

new Bar().sum(); // Logs 8 (5 + 3);
// With types:

class Foo {
  x: ?number = 2;
  y: number = 3;
}

class Bar extends Foo {
  // *

  sum() {
    const sum: number = (this.x || 5) + this.y;
    console.log(sum);
  }
}

new Bar().sum(); // Should log 8 (5 + 3);

* I want to say that the Bar class has:

  • an x property whose type is ?number and that is part of the JS code
  • an y property whose type is number and that isn't part of the JS code.

How can I differentiate the two cases?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions