It'd be great if Flow could warn if one tries to overwrite a variable that has been declared to be "read-only".
For example, suppose the ! character was introduced into type declarations to indicate "read-only" state; one could then do:
////////// Foo.js //////////
export default class Foo
{
bar: !string; // read-only property
constructor(bar: !string) // read-only parameter
{
this.bar = bar; // read-only properties may be assigned in constructor
// (or perhaps from any class method)
bar = 'quz'; // Flow violation
}
}
////////// main.js //////////
import Foo from './Foo';
const foo = new Foo('qux');
foo.bar = 'quz'; // Flow violation
Yeah, something like this is very much on the todo list. Will probably use const though.
@avikchaudhuri Will this be landing on v0.37? Currently(0.36), seems const can not be used inside class .
Flow 0.34 added covariant properties, which are _kind of_ like read-only properties. Not exactly the same. But it can help for some use cases. Check it out in the docs here: https://flowtype.org/docs/variance.html
Is it possible to use covariant properties and intersection types to get read-only? {x : +a} & {x: a} seems to do the right thing.
You can use covariant properties now on classes 馃憤
Most helpful comment
Yeah, something like this is very much on the todo list. Will probably use
constthough.