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
TypeScript 4.0: Support labeled tuple elements #11754
TypeScript 4.0: Support labeled tuple elements #11754
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
@@ -76,6 +76,8 @@ const TSErrors = Object.freeze({ | |||
IndexSignatureHasStatic: "Index signatures cannot have the 'static' modifier", | |||
OptionalTypeBeforeRequired: | |||
"A required element cannot follow an optional element.", | |||
LabeledElementBeforeUnlabeled: | |||
"An unlabeled element cannot follow a labeled element.", |
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.
TypeScript throws Tuple members must all have names or all not have names
. However, it accepts this code:
type B = [A, x: C];
@weswigham / @DanielRosenwasser Which is the correct behavior?
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.
Uhh.. we shouldn't accept that (but it is just a grammar error, not a parse error). Care to open an issue?
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/25871/ |
ce28128
to
016add4
Compare
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.
Can you add a test case on named rest tuple members
type T = [x: A, ...y: B]
and also an invalid case on optional rest named tuple members
type T = [x: A, ...y?: B]
on which tsc throws
A tuple member cannot be both optional and rest.(5085)
Oh I completely missed rest labeled elements. |
Should the AST be a |
extend interface TSRestType {
typeAnnotation: TSType | TSNamedTupleMember;
} looks good to me, and it is consistent with how we model |
packages/babel-parser/src/types.js
Outdated
elementTypes: $ReadOnlyArray<TsType | TsTupleElementType>, | ||
}; | ||
|
||
export type TsNamedTupleMember = TsTypeBase & { |
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.
Since it is a new node type, we should also add support on @babel/generator
.
|
||
const isLabeled = type === "TSNamedTupleMember"; | ||
// Flow doesn't support ??= | ||
labeledElements = labeledElements ?? isLabeled; |
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.
labeledElements = labeledElements ?? isLabeled; | |
labeledElements ?? (labeledElements = isLabeled); |
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.
Even if it's one operation more, I find the = ... ??
more readable
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.
😂 But that's not the intended semantics of ||=
. They are just nitpicks, feel free to ignore them if current code looks good to you.
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.
In this case, without with
or MemberExpression
, it has exactly the same behavior 😛
seenOptionalElement = | ||
seenOptionalElement || | ||
(type === "TSNamedTupleMember" && elementNode.optional) || | ||
type === "TSOptionalType"; |
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.
seenOptionalElement = | |
seenOptionalElement || | |
(type === "TSNamedTupleMember" && elementNode.optional) || | |
type === "TSOptionalType"; | |
seenOptionalElement || | |
(seenOptionalElement = | |
(type === "TSNamedTupleMember" && elementNode.optional) || | |
type === "TSOptionalType"); |
@existentialism I think this can be merged because it targets to |
* TypeScript 4.0: Support labeled tuple elements * More tests * Disallow mixing labeled and unlabeled elements * Update AST shape * Enable test after rebase * Allow labeled spread types * Fix flow * Add types and generator suport * Update packages/babel-parser/src/plugins/typescript/index.js * Prettier
* TypeScript 4.0: Support labeled tuple elements * More tests * Disallow mixing labeled and unlabeled elements * Update AST shape * Enable test after rebase * Allow labeled spread types * Fix flow * Add types and generator suport * Update packages/babel-parser/src/plugins/typescript/index.js * Prettier
https://devblogs.microsoft.com/typescript/announcing-typescript-4-0-beta/#labeled-tuple-elements