diff --git a/changelog_unreleased/flow/15466.md b/changelog_unreleased/flow/15466.md new file mode 100644 index 000000000..0ab08d6a9 --- /dev/null +++ b/changelog_unreleased/flow/15466.md @@ -0,0 +1,8 @@ +#### Support type arguments after `typeof` (#15466 by @sosukesuzuki) + +Supports type arguments after `typeof` syntax supported since [Flow v0.127.0](https://github.com/facebook/flow/blob/HEAD/Changelog.md#02170): + + +```jsx +type Foo = typeof MyGenericClass; +``` diff --git a/src/language-js/print/type-annotation.js b/src/language-js/print/type-annotation.js index 57620bf6c..286d7d780 100644 --- a/src/language-js/print/type-annotation.js +++ b/src/language-js/print/type-annotation.js @@ -521,12 +521,11 @@ function printArrayType(print) { - `TypeofTypeAnnotation` */ function printTypeQuery({ node }, print) { - return [ - "typeof ", - ...(node.type === "TSTypeQuery" - ? [print("exprName"), print("typeParameters")] - : [print("argument")]), - ]; + const argumentPropertyName = + node.type === "TSTypeQuery" ? "exprName" : "argument"; + const typeArgsPropertyName = + node.type === "TSTypeQuery" ? "typeParameters" : "typeArguments"; + return ["typeof ", print(argumentPropertyName), print(typeArgsPropertyName)]; } function printTypePredicate(path, print) { diff --git a/src/language-js/traverse/visitor-keys.evaluate.js b/src/language-js/traverse/visitor-keys.evaluate.js index c07aa460e..d0e8121d1 100644 --- a/src/language-js/traverse/visitor-keys.evaluate.js +++ b/src/language-js/traverse/visitor-keys.evaluate.js @@ -43,6 +43,7 @@ const additionalVisitorKeys = { AsExpression: ["expression", "typeAnnotation"], AsConstExpression: ["expression"], SatisfiesExpression: ["expression", "typeAnnotation"], + TypeofTypeAnnotation: ["argument", "typeArguments"], }; const excludeKeys = { diff --git a/tests/format/flow/typeof/__snapshots__/jsfmt.spec.js.snap b/tests/format/flow/typeof/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000..d0df803f0 --- /dev/null +++ b/tests/format/flow/typeof/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,15 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`typeof-with-type-arguments.js format 1`] = ` +====================================options===================================== +parsers: ["flow"] +printWidth: 80 + | printWidth +=====================================input====================================== +type Foo = typeof MyGenericClass; + +=====================================output===================================== +type Foo = typeof MyGenericClass; + +================================================================================ +`; diff --git a/tests/format/flow/typeof/jsfmt.spec.js b/tests/format/flow/typeof/jsfmt.spec.js new file mode 100644 index 000000000..c89f3cc86 --- /dev/null +++ b/tests/format/flow/typeof/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(import.meta, ["flow"]); diff --git a/tests/format/flow/typeof/typeof-with-type-arguments.js b/tests/format/flow/typeof/typeof-with-type-arguments.js new file mode 100644 index 000000000..53f468098 --- /dev/null +++ b/tests/format/flow/typeof/typeof-with-type-arguments.js @@ -0,0 +1 @@ +type Foo = typeof MyGenericClass; diff --git a/tests/unit/__snapshots__/visitor-keys.js.snap b/tests/unit/__snapshots__/visitor-keys.js.snap index eea4e0d6b..ca034cdaf 100644 --- a/tests/unit/__snapshots__/visitor-keys.js.snap +++ b/tests/unit/__snapshots__/visitor-keys.js.snap @@ -1043,6 +1043,7 @@ exports[`visitor keys estree 1`] = ` ], "TypeofTypeAnnotation": [ "argument", + "typeArguments", ], "UnaryExpression": [ "argument",