43

I came across the following code written in js FlowType (I am interested to know the value of + in the context of FlowType not in general JS).

Could you please explain me what does the + symbol mean in front of the property in the code below:

  export type User = {
      +name: string,
      +surname: string,
      +personId: PourceId,
    }

I could not find any reference in the documentation, any link is also welcome.

GibboK
  • 71,848
  • 143
  • 435
  • 658

3 Answers3

58

The + symbol in front of the property means the property is read-only

Reference: https://flow.org/en/docs/types/interfaces/#toc-interface-property-variance-read-only-and-write-only

MichaelDeBoey
  • 2,335
  • 1
  • 19
  • 19
13

The '+' symbol means the property is read only and
'-' means the property is write only and
If does not have any '+' or '-' symbol it means the property have both read/write access.
It can be used while defining interface property or type property.

The documentation can be found in below link:
https://flow.org/en/docs/types/interfaces/#toc-interface-property-variance-read-only-and-write-only

kalimsayyad
  • 1,076
  • 8
  • 9
5

After some additional research I found out that +/- indicate the covariant or contravariant for a property.

interface MyInterface {
  +covariant: number;     // read-only
  -contravariant: number; // write-only
}

Interesting article on variance:

https://flow.org/en/docs/lang/variance/

https://flow.org/en/docs/types/interfaces/#toc-interface-property-variance-read-only-and-write-only

GibboK
  • 71,848
  • 143
  • 435
  • 658