Skip to content

Commit

Permalink
add union types
Browse files Browse the repository at this point in the history
  • Loading branch information
gfx committed Aug 9, 2024
1 parent bb85676 commit b7b673f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,11 @@ This is a list of features in TypeScript that are planned or implemented:
* [x] `satisfies` type operator
* [x] primitive type annotations
* [ ] literal type annotations
* [ ] union type annotations
* [x] union type annotations
* [ ] intersection type annotations
* [ ] tuple type annotations
* [ ] array type annotations
* [ ] [WIP] object type annotations
* [x] object type annotations
* [ ] type guards
* [ ] `interface` type statement
* [x] `type` type statement
Expand Down
5 changes: 5 additions & 0 deletions spec/type_union.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type Foo = string | number | boolean;

export const a: Foo = 'foo';
export const b: Foo = 42;
export const c: Foo = true;
5 changes: 5 additions & 0 deletions spec/type_union.ts.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"a": "foo",
"b": 42,
"c": true
}
8 changes: 7 additions & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,13 @@ fn type_object(input: Span) -> IResult<Span, TypeDecl> {

fn type_expr(i: Span) -> IResult<Span, TypeDecl> {
let (i, td) = alt((type_primitive, type_object))(i)?;
Ok((i, td))

let Ok((i, _)) = space_delimited(char::<Span, Error>('|'))(i) else {
return Ok((i, td))
};

let (i, _rhs) = type_expr(i)?;
Ok((i, TypeDecl::Any)) // union, but not yet implemented
}

fn argument(i: Span) -> IResult<Span, (Span, TypeDecl)> {
Expand Down

0 comments on commit b7b673f

Please sign in to comment.