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

Suggestion: Allowing leading pipe in union type declarations #12071

Closed
mariusschulz opened this issue Nov 6, 2016 · 4 comments
Closed

Suggestion: Allowing leading pipe in union type declarations #12071

mariusschulz opened this issue Nov 6, 2016 · 4 comments
Labels
Fixed A PR has been merged for this issue Help Wanted You can do this Suggestion An idea for TypeScript

Comments

@mariusschulz
Copy link
Contributor

Currently, because of the leading | symbol , the following code is invalid TypeScript:

type ReduxAction =
  | { type: "INCREMENT" }
  | { type: "DECREMENT" }
  | { type: "SET_VALUE", value: number };

I suggest we relax the syntax such that a single leading pipe symbol is allowed (but not required) in a union type declaration. Flow and F#, for example, both allow this syntax.

This change is backwards-compatible and gives developers a little more code formatting freedom.

@DanielRosenwasser DanielRosenwasser added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Nov 6, 2016
@RyanCavanaugh RyanCavanaugh added Help Wanted You can do this and removed In Discussion Not yet reached consensus labels Nov 15, 2016
@RyanCavanaugh RyanCavanaugh added this to the Community milestone Nov 15, 2016
@RyanCavanaugh
Copy link
Member

Accepting PRs to allow one leading ignored | or & in a type position

@mariusschulz
Copy link
Contributor Author

@RyanCavanaugh If I'm not mistaken, parsing a leading | or & boils down to the following change:

function parseUnionOrIntersectionType(kind: SyntaxKind, parseConstituentType: () => TypeNode, operator: SyntaxKind): TypeNode {
+   parseOptional(operator);
    let type = parseConstituentType();
    if (token() === operator) {
        const types = createNodeArray<TypeNode>([type], type.pos);
        while (parseOptional(operator)) {
            types.push(parseConstituentType());
        }
        types.end = getNodeEnd();
        const node = <UnionOrIntersectionTypeNode>createNode(kind, type.pos);
        node.types = types;
        type = finishNode(node);
    }
    return type;
}

Where exactly would an accompanying test go?

@RyanCavanaugh
Copy link
Member

@mariusschulz you can drop a new file in /tests/cases/compiler/. See CONTRIBUTING.md for more details.

@mariusschulz
Copy link
Contributor Author

@RyanCavanaugh Thanks! I've sent a pull request: #12386

@RyanCavanaugh RyanCavanaugh added the Fixed A PR has been merged for this issue label Nov 29, 2016
@mhegazy mhegazy modified the milestones: TypeScript 2.1.3, Community Nov 29, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Fixed A PR has been merged for this issue Help Wanted You can do this Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants