update fn return type

This commit is contained in:
Tom Elliott 2024-07-24 10:29:37 +12:00
parent bb8b2323e8
commit fa5be168c0
3 changed files with 10 additions and 7 deletions

View File

@ -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")
}

View File

@ -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)

View File

@ -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<Numeric>;
```