From 74923fa8316cd78f8461e77290915155efbcd714 Mon Sep 17 00:00:00 2001 From: fisker Date: Fri, 26 Jun 2026 20:40:05 +0800 Subject: [PATCH] Fix variance annotation --- changelog_unreleased/flow/19022.md | 19 +++++++++++++++++++ src/language-js/print/flow.js | 11 +++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 changelog_unreleased/flow/19022.md diff --git a/changelog_unreleased/flow/19022.md b/changelog_unreleased/flow/19022.md new file mode 100644 index 000000000..7b26a5c17 --- /dev/null +++ b/changelog_unreleased/flow/19022.md @@ -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). + + +```jsx +// Input +type T = { + readonly foo: string, +}; + +// Prettier stable +SyntaxError + +// Prettier main +type T = { + readonly foo: string, +}; +``` \ No newline at end of file diff --git a/src/language-js/print/flow.js b/src/language-js/print/flow.js index 24230abfb..836e9bd48 100644 --- a/src/language-js/print/flow.js +++ b/src/language-js/print/flow.js @@ -201,8 +201,15 @@ function printFlow(path, options, print) { 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" || + kind === "writeonly" || + kind === "in" || + kind === "out", + ); + return kind === "plus" ? "+" : kind === "minus" ? "-" : `${kind} `; } case "KeyofTypeAnnotation": return ["keyof ", print("argument")];