From fa5be168c04124cdc2b202877c9ea0f1527e1fd2 Mon Sep 17 00:00:00 2001 From: Tom Elliott Date: Wed, 24 Jul 2024 10:29:37 +1200 Subject: [PATCH] update fn return type --- R/compile.R | 2 +- R/types.R | 13 ++++++++----- README.md | 2 +- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/R/compile.R b/R/compile.R index 8f8f40e..4c71745 100644 --- a/R/compile.R +++ b/R/compile.R @@ -7,5 +7,5 @@ ts_compile <- function(f) { inputs <- sapply(inputs, \(x) x$type) fn_args <- paste(names(inputs), inputs, sep = ": ") |> paste(collapse = ", ") - cat(sprintf("const %s = (%s) => %s;", name, fn_args, result$type_fn), "\n") + cat(sprintf("const %s = (%s) => Promise<%s>;", name, fn_args, result$type_fn), "\n") } diff --git a/R/types.R b/R/types.R index bb861de..fe831a3 100644 --- a/R/types.R +++ b/R/types.R @@ -36,14 +36,17 @@ n_type <- function(n, type, pl = ts_array(type)) { pl } n_type_fun <- function(n, type) { - sprintf("%s(%s)", type, ifelse(n < 0, "", n)) + if (n < 0) { + return(type) + } + sprintf("%s<%s>)", type, n) } #' @export ts_logical <- function(n = -1L) { object( n_type(n, "boolean"), - n_type_fun(n, "logical"), + n_type_fun(n, "Logical"), check = function(x) { if (!is.logical(x)) stop("Expected a boolean") if (n > 0 && length(x) != n) stop("Expected a boolean of length ", n) @@ -56,7 +59,7 @@ ts_logical <- function(n = -1L) { ts_integer <- function(n = -1L) { object( n_type(n, "number"), - n_type_fun(n, "integer"), + n_type_fun(n, "Integer"), check = function(x) { if (!is.integer(x)) stop("Expected an integer") if (n > 0 && length(x) != n) stop("Expected an integer of length ", n) @@ -69,7 +72,7 @@ ts_integer <- function(n = -1L) { ts_numeric <- function(n = -1L) { object( n_type(n, "number"), - n_type_fun(n, "numeric"), + n_type_fun(n, "Numeric"), check = function(x) { if (!is.numeric(x)) stop("Expected a number") if (n > 0 && length(x) != n) stop("Expected a number of length ", n) @@ -82,7 +85,7 @@ ts_numeric <- function(n = -1L) { ts_character <- function(n = -1L) { object( n_type(n, "string"), - n_type_fun(n, "character"), + n_type_fun(n, "Character"), check = function(x) { if (!is.character(x)) stop("Expected a string") if (n > 0 && length(x) != n) stop("Expected a string of length ", n) diff --git a/README.md b/README.md index ec3dd30..530fde5 100644 --- a/README.md +++ b/README.md @@ -92,5 +92,5 @@ library(ts) myfun <- ts_function(mean, x = ts_numeric(), result = ts_numeric()) ts_compile(myfun) -#> const myfun = (x: number | number[]) => numeric(); +#> const myfun = (x: number | number[]) => Promise; ```