New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support TS4.9 satisfies
operator
#13764
Changes from 18 commits
7211cba
6ce205a
29cdbc4
ebbb90a
336bc4c
bdaf5c1
096969a
0eaedd7
dc1b41f
cd19ebf
93b886a
e05ea2c
e76e848
911c47a
a148674
b2aa3f2
a7f78e8
1173f55
966c5df
e3e112d
fcba739
bf0daa9
5f31b99
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#### Support TypeScript 4.9 (#13764 by @sosukesuzuki) | ||
|
||
Support [TypeScript 4.9](https://devblogs.microsoft.com/typescript/announcing-typescript-4-9) features! | ||
|
||
##### [`satisfies` operator](https://devblogs.microsoft.com/typescript/announcing-typescript-4-9-beta/#the-satisfies-operator) | ||
|
||
Supported by only `babel-ts` parser. | ||
|
||
<!-- prettier-ignore --> | ||
```tsx | ||
const palette = { | ||
red: [255, 0, 0], | ||
green: "#00ff00", | ||
blue: [0, 0, 255] | ||
} satisfies Record<Colors, string | RGB>; | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ import { | |
isCallExpression, | ||
isMemberExpression, | ||
isObjectProperty, | ||
isTSTypeExpression, | ||
} from "./utils/index.js"; | ||
|
||
function needsParens(path, options) { | ||
|
@@ -227,14 +228,19 @@ function needsParens(path, options) { | |
// fallthrough | ||
case "TSTypeAssertion": | ||
case "TSAsExpression": | ||
case "TSSatisfiesExpression": | ||
case "LogicalExpression": | ||
switch (parent.type) { | ||
case "TSAsExpression": | ||
// example: foo as unknown as Bar | ||
return node.type !== "TSAsExpression"; | ||
|
||
case "TSSatisfiesExpression": | ||
// example: foo satisfies unknown satisfies Bar | ||
return node.type !== "TSSatisfiesExpression"; | ||
|
||
case "ConditionalExpression": | ||
return node.type === "TSAsExpression"; | ||
return isTSTypeExpression(node); | ||
|
||
case "CallExpression": | ||
case "NewExpression": | ||
|
@@ -265,7 +271,12 @@ function needsParens(path, options) { | |
case "AssignmentPattern": | ||
return ( | ||
key === "left" && | ||
(node.type === "TSTypeAssertion" || node.type === "TSAsExpression") | ||
(node.type === "TSTypeAssertion" || | ||
node.type === "TSAsExpression" || | ||
// babel-parser cannot parse `satisfies` operator in left side of assignment | ||
// https://github.com/babel/babel/issues/15095 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
// TODO: Add tests after the bug is fixed | ||
node.type === "TSSatisfiesExpression") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can reuse |
||
); | ||
|
||
case "LogicalExpression": | ||
|
@@ -360,6 +371,7 @@ function needsParens(path, options) { | |
case "SpreadElement": | ||
case "SpreadProperty": | ||
case "TSAsExpression": | ||
case "TSSatisfiesExpression": | ||
case "TSNonNullExpression": | ||
case "BindExpression": | ||
return true; | ||
|
@@ -592,6 +604,7 @@ function needsParens(path, options) { | |
case "TSTypeAssertion": | ||
case "TypeCastExpression": | ||
case "TSAsExpression": | ||
case "TSSatisfiesExpression": | ||
case "TSNonNullExpression": | ||
return true; | ||
|
||
|
@@ -641,6 +654,7 @@ function needsParens(path, options) { | |
return key === "object"; | ||
|
||
case "TSAsExpression": | ||
case "TSSatisfiesExpression": | ||
case "TSNonNullExpression": | ||
case "BindExpression": | ||
case "TaggedTemplateExpression": | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -515,6 +515,8 @@ function printTypescript(path, options, print) { | |
return printJSDocType(path, print, /* token */ "!"); | ||
case "TSInstantiationExpression": | ||
return [print("expression"), print("typeParameters")]; | ||
case "TSSatisfiesExpression": | ||
return [print("expression"), " satisfies ", print("typeAnnotation")]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Merge this into There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
default: | ||
/* c8 ignore next */ | ||
throw new UnexpectedNodeError(node, "TypeScript"); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe mixed should not add parens either
Prettier pr-13764
Playground link
Input:
Output: