Skip to content
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

Merged

Conversation

nicolo-ribaudo
Copy link
Member

@nicolo-ribaudo nicolo-ribaudo commented Jun 27, 2020

Q                       A
Fixed Issues?
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature? Yes
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

https://devblogs.microsoft.com/typescript/announcing-typescript-4-0-beta/#labeled-tuple-elements

@nicolo-ribaudo nicolo-ribaudo added PR: New Feature 🚀 A type of pull request used for our changelog categories area: typescript labels Jun 27, 2020
@nicolo-ribaudo nicolo-ribaudo added this to the 7.11.0 milestone Jun 27, 2020
@codesandbox-ci
Copy link

codesandbox-ci bot commented Jun 27, 2020

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.",
Copy link
Member Author

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];

Playground Link

@weswigham / @DanielRosenwasser Which is the correct behavior?

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?

@babel-bot
Copy link
Collaborator

babel-bot commented Jun 27, 2020

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/25871/

@nicolo-ribaudo nicolo-ribaudo changed the base branch from main to feat-7.11.0/ts-4.0 June 29, 2020 23:08
@nicolo-ribaudo nicolo-ribaudo mentioned this pull request Jun 29, 2020
4 tasks
Copy link
Contributor

@JLHwung JLHwung left a 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)

@nicolo-ribaudo
Copy link
Member Author

Oh I completely missed rest labeled elements.

@nicolo-ribaudo
Copy link
Member Author

Should the AST be a TSNamedTupleMember inside a TSRestElement, or the other way around?

@JLHwung
Copy link
Contributor

JLHwung commented Jun 30, 2020

Should the AST be a TSNamedTupleMember inside a TSRestElement, or the other way around?

extend interface TSRestType {
  typeAnnotation: TSType | TSNamedTupleMember;
}

looks good to me, and it is consistent with how we model type A = [...B] by placing B in the typeAnnotation.

elementTypes: $ReadOnlyArray<TsType | TsTupleElementType>,
};

export type TsNamedTupleMember = TsTypeBase & {
Copy link
Contributor

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
labeledElements = labeledElements ?? isLabeled;
labeledElements ?? (labeledElements = isLabeled);

Copy link
Member Author

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

Copy link
Contributor

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.

Copy link
Member Author

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 😛

Comment on lines +654 to +657
seenOptionalElement =
seenOptionalElement ||
(type === "TSNamedTupleMember" && elementNode.optional) ||
type === "TSOptionalType";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
seenOptionalElement =
seenOptionalElement ||
(type === "TSNamedTupleMember" && elementNode.optional) ||
type === "TSOptionalType";
seenOptionalElement ||
(seenOptionalElement =
(type === "TSNamedTupleMember" && elementNode.optional) ||
type === "TSOptionalType");

@existentialism existentialism added the PR: Ready to be Merged A pull request with already two approvals, but waiting for the next minor release label Jul 14, 2020
@JLHwung
Copy link
Contributor

JLHwung commented Jul 14, 2020

@existentialism I think this can be merged because it targets to feat-7.11.0/ts-4.0 instead of main.

@JLHwung JLHwung removed this from the 7.11.0 milestone Jul 14, 2020
@JLHwung JLHwung removed the PR: Ready to be Merged A pull request with already two approvals, but waiting for the next minor release label Jul 14, 2020
@JLHwung JLHwung merged commit 309536a into babel:feat-7.11.0/ts-4.0 Jul 14, 2020
@nicolo-ribaudo nicolo-ribaudo deleted the ts-4.0/labeled-tuple-elements branch July 15, 2020 05:49
nicolo-ribaudo added a commit that referenced this pull request Jul 15, 2020
* 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
JLHwung pushed a commit that referenced this pull request Jul 29, 2020
* 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
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Oct 15, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 15, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: typescript outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: New Feature 🚀 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants