Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 13.7k
Union Types#824
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
Uh oh!
There was an error while loading. Please reload this page.
Union Types #824
Changes from all commits
e836fe1d70494fb8923b35669f63c439ae495584e9fd5b8083a17b02ea4cbbe5c661ba779db6e2eb51abdc43e83927f04f9f43ac0bacb9d08ce1760f5a9fee483afea4e02b9fc9a42c12ce627c4442b4504e5309eee160283d9aeda76a418869ee41fc842b14f4f59af5cd414File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -225,6 +225,8 @@ module ts { | ||
| return child((<ArrayTypeNode>node).elementType); | ||
| case SyntaxKind.TupleType: | ||
| return children((<TupleTypeNode>node).elementTypes); | ||
| case SyntaxKind.UnionType: | ||
| return children((<UnionTypeNode>node).types); | ||
| case SyntaxKind.ArrayLiteral: | ||
| return children((<ArrayLiteral>node).elements); | ||
| case SyntaxKind.ObjectLiteral: | ||
| @@ -1729,9 +1731,9 @@ module ts { | ||
| } | ||
| } | ||
| function parseType(): TypeNode { | ||
| function parseNonUnionType(): TypeNode { | ||
| var type = parseNonArrayType(); | ||
| while (type && !scanner.hasPrecedingLineBreak() && parseOptional(SyntaxKind.OpenBracketToken)) { | ||
| while (!scanner.hasPrecedingLineBreak() && parseOptional(SyntaxKind.OpenBracketToken)) { | ||
| parseExpected(SyntaxKind.CloseBracketToken); | ||
| var node = <ArrayTypeNode>createNode(SyntaxKind.ArrayType, type.pos); | ||
| node.elementType = type; | ||
| @@ -1740,6 +1742,22 @@ module ts { | ||
| return type; | ||
| } | ||
| function parseType(): TypeNode { | ||
Contributor 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. right now it looks like it's not possible to have syntax for an array of union types. That seems like it could be problematic in the future. For example, say you have htis in your .js: This function will have a type that is nonexpressable with the syntax of the language (and thus would be a problem for .d.ts files, as well as anyone who wants to explicitly give typings to things). Member 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 write this as Contributor 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. Great point. We'll likely have to do that if we don't make additional syntax. As htis is a perfectly reasonable workaround, i'd prefer this approach before adding more syntax. thanks! 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. It would be great to have union type aliases, to keep the length of type names managable: Other values for the define keyword could be alias, type or class 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. I imagine this will fall out as a default feature, based on the type inference system of TypeScript, but I think it bears repeating. Union types should have anonymous interfaces: The anonymous interface would have the form: Or go wild and give this interface a name like: This does not have any use case I can think of, but perhaps I'm not thinking hard enough. Daniel Rosenwasser (@DanielRosenwasser) Yes I do. So consider this an upvote. =) Member 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. Davide Rubino (@drubino) you mean like in #957? 😃 This is a relatively old conversation at this point - you'll find quite a bit has happened recently with respect to union types, such as #914. 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. Haskell has union types denoted by Something like: When combined with the anonymous interface idea, along with syntax to combine or skip blocks if logic can be implemented by common members in certain cases, you might be able enable the user to be quite succinct for common scenarios. 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. It might also be interesting to have a way of defining union types. Perhaps there is one master type that all others can be cast to, and the cast is specified in the definition. The compiler would then handle optimizations where casting can be proven to be unnecessary: This is probably a bit more complex than the design team had in mind for union types, but I thought I'd throw it out there all the same. I believe this, or features like it, would provide us with language constructs that would help to bridge the gap between dynamically typed languages and statically typed ones. | ||
| var type = parseNonUnionType(); | ||
| if (token === SyntaxKind.BarToken) { | ||
| var types = <NodeArray<TypeNode>>[type]; | ||
| types.pos = type.pos; | ||
| while (parseOptional(SyntaxKind.BarToken)) { | ||
| types.push(parseNonUnionType()); | ||
| } | ||
| types.end = getNodeEnd(); | ||
| var node = <UnionTypeNode>createNode(SyntaxKind.UnionType, type.pos); | ||
| node.types = types; | ||
| type = finishNode(node); | ||
| } | ||
| return type; | ||
| } | ||
| function parseTypeAnnotation(): TypeNode { | ||
| return parseOptional(SyntaxKind.ColonToken) ? parseType() : undefined; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
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.
'distinct'.
'deduplicate' indicates you are mutating in the array in place. 'distinct' is the linq name for getting back the set of unique elements.