-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Union Types #824
Union Types #824
Changes from all commits
e836fe1
d70494f
b8923b3
5669f63
c439ae4
95584e9
fd5b808
3a17b02
ea4cbbe
5c661ba
779db6e
2eb51ab
dc43e83
927f04f
9f43ac0
bacb9d0
8ce1760
f5a9fee
483afea
4e02b9f
c9a42c1
2ce627c
4442b45
04e5309
eee1602
83d9aed
a76a418
869ee41
fc842b1
4f4f59a
f5cd414
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
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 { | ||
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). 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 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. @DanielRosenwasser Yes I do. So consider this an upvote. =) 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. 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; | ||
} | ||
|
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.