Skip to content

Commit

Permalink
Improve TypeScript Array Output Types (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
sasial-dev authored Dec 28, 2023
1 parent 2df5633 commit 8ee3a11
Showing 1 changed file with 79 additions and 4 deletions.
83 changes: 79 additions & 4 deletions zap/src/output/typescript/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,85 @@ pub trait Output {
Ty::Num(..) => self.push("number"),
Ty::Str { .. } => self.push("string"),

Ty::Arr(ty, ..) => {
self.push_ty(ty);
self.push("[]");
}
Ty::Arr(ty, range) => match (range.min(), range.max()) {
(Some(min), Some(max)) => {
if let Some(exact) = range.exact() {
self.push("[");

for i in 0..exact as usize {
if i != 0 {
self.push(", ");
}

self.push_ty(ty);
}

self.push("]");
} else {
if min as usize != 0 {
self.push("[");

for i in 0..min as usize {
if i != 0 {
self.push(", ");
}

self.push_ty(ty);
}

self.push("] & ");
}

self.push("Partial<[");

for i in 0..max as usize {
if i != 0 {
self.push(", ");
}

self.push_ty(ty);
}

self.push("]>");
}
}
(Some(min), None) => {
self.push("[");

if min as usize != 0 {
for i in 0..min as usize {
if i != 0 {
self.push(", ");
}

self.push_ty(ty);
}

self.push(", ");
}

self.push("...Array<");
self.push_ty(ty);
self.push(" | undefined>]");
}
(None, Some(max)) => {
self.push("Partial<[");

for i in 0..max as usize {
if i != 0 {
self.push(", ");
}

self.push_ty(ty);
}

self.push("]>");
}
_ => {
self.push_ty(ty);
self.push("[]");
}
},

Ty::Map(key, val) => {
self.push("{ [index: ");
Expand Down

0 comments on commit 8ee3a11

Please sign in to comment.