Support readonly as Flow variance annotation (#19022)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: fisker Cheung <lionkay@gmail.com>
This commit is contained in:
5 files changed
+67
-3
No files matched your search
@@ -0,0 +1,19 @@
|
||||
#### Support `readonly` as a variance annotation (#19022 by @marcoww6)
|
||||
|
||||
Flow now accepts `readonly` as a property variance annotation, equivalent to `+` (covariant/read-only).
|
||||
|
||||
<!-- prettier-ignore -->
|
||||
```jsx
|
||||
// Input
|
||||
type T = {
|
||||
readonly foo: string,
|
||||
};
|
||||
|
||||
// Prettier stable
|
||||
SyntaxError
|
||||
|
||||
// Prettier main
|
||||
type T = {
|
||||
readonly foo: string,
|
||||
};
|
||||
```
|
||||
@@ -218,8 +218,8 @@ function printFlow(path, options, print, args) {
|
||||
return ["?", print("typeAnnotation")];
|
||||
case "Variance": {
|
||||
const { kind } = node;
|
||||
assert.ok(kind === "plus" || kind === "minus");
|
||||
return kind === "plus" ? "+" : "-";
|
||||
assert.ok(kind === "plus" || kind === "minus" || kind === "readonly");
|
||||
return kind === "plus" ? "+" : kind === "minus" ? "-" : `${kind} `;
|
||||
}
|
||||
case "KeyofTypeAnnotation":
|
||||
return ["keyof ", print("argument")];
|
||||
|
||||
+1
-1
@@ -1200,7 +1200,7 @@ export type TypeAnnotationType =
|
||||
|
||||
export interface Variance extends BaseNode {
|
||||
type: "Variance";
|
||||
kind: "plus" | "minus";
|
||||
kind: "plus" | "minus" | "readonly";
|
||||
}
|
||||
|
||||
interface BaseTypeAlias extends BaseNode {
|
||||
|
||||
@@ -9,10 +9,40 @@ class Route {
|
||||
static +param: T;
|
||||
}
|
||||
|
||||
class ReadonlyRoute {
|
||||
static readonly param: T;
|
||||
}
|
||||
|
||||
type ReadonlyObj = {
|
||||
readonly foo: string,
|
||||
readonly [string]: unknown
|
||||
};
|
||||
|
||||
type ReadonlyTuple = [ readonly label: number ];
|
||||
|
||||
type ReadonlyInterface = interface {
|
||||
readonly prop: string
|
||||
};
|
||||
|
||||
=====================================output=====================================
|
||||
class Route {
|
||||
static +param: T;
|
||||
}
|
||||
|
||||
class ReadonlyRoute {
|
||||
static readonly param: T;
|
||||
}
|
||||
|
||||
type ReadonlyObj = {
|
||||
readonly foo: string,
|
||||
readonly [string]: unknown,
|
||||
};
|
||||
|
||||
type ReadonlyTuple = [readonly label: number];
|
||||
|
||||
type ReadonlyInterface = interface {
|
||||
readonly prop: string,
|
||||
};
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
@@ -1,3 +1,18 @@
|
||||
class Route {
|
||||
static +param: T;
|
||||
}
|
||||
|
||||
class ReadonlyRoute {
|
||||
static readonly param: T;
|
||||
}
|
||||
|
||||
type ReadonlyObj = {
|
||||
readonly foo: string,
|
||||
readonly [string]: unknown
|
||||
};
|
||||
|
||||
type ReadonlyTuple = [ readonly label: number ];
|
||||
|
||||
type ReadonlyInterface = interface {
|
||||
readonly prop: string
|
||||
};
|
||||
Reference in new issue
Block a user