110 lines
6.9 KiB
HTML
110 lines
6.9 KiB
HTML
<!DOCTYPE html>
|
|
<!-- Generated by pkgdown: do not edit by hand --><html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"><title>Types in R and TypeScript — type_objects • ts</title><script src="../deps/jquery-3.6.0/jquery-3.6.0.min.js"></script><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"><link href="../deps/bootstrap-5.3.1/bootstrap.min.css" rel="stylesheet"><script src="../deps/bootstrap-5.3.1/bootstrap.bundle.min.js"></script><link href="../deps/font-awesome-6.5.2/css/all.min.css" rel="stylesheet"><link href="../deps/font-awesome-6.5.2/css/v4-shims.min.css" rel="stylesheet"><script src="../deps/headroom-0.11.0/headroom.min.js"></script><script src="../deps/headroom-0.11.0/jQuery.headroom.min.js"></script><script src="../deps/bootstrap-toc-1.0.1/bootstrap-toc.min.js"></script><script src="../deps/clipboard.js-2.0.11/clipboard.min.js"></script><script src="../deps/search-1.0.0/autocomplete.jquery.min.js"></script><script src="../deps/search-1.0.0/fuse.min.js"></script><script src="../deps/search-1.0.0/mark.min.js"></script><!-- pkgdown --><script src="../pkgdown.js"></script><meta property="og:title" content="Types in R and TypeScript — type_objects"><meta name="description" content="This document provides an overview of the main types available to
|
|
app developers, which includes the main types in R and their
|
|
TypeScript counterparts."><meta property="og:description" content="This document provides an overview of the main types available to
|
|
app developers, which includes the main types in R and their
|
|
TypeScript counterparts."></head><body>
|
|
<a href="#main" class="visually-hidden-focusable">Skip to contents</a>
|
|
|
|
|
|
<nav class="navbar navbar-expand-lg fixed-top bg-light" data-bs-theme="light" aria-label="Site navigation"><div class="container">
|
|
|
|
<a class="navbar-brand me-2" href="../index.html">ts</a>
|
|
|
|
<small class="nav-text text-muted me-auto" data-bs-toggle="tooltip" data-bs-placement="bottom" title="">0.1.0</small>
|
|
|
|
|
|
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbar" aria-controls="navbar" aria-expanded="false" aria-label="Toggle navigation">
|
|
<span class="navbar-toggler-icon"></span>
|
|
</button>
|
|
|
|
<div id="navbar" class="collapse navbar-collapse ms-3">
|
|
<ul class="navbar-nav me-auto"><li class="active nav-item"><a class="nav-link" href="../reference/index.html">Reference</a></li>
|
|
<li class="nav-item dropdown">
|
|
<button class="nav-link dropdown-toggle" type="button" id="dropdown-articles" data-bs-toggle="dropdown" aria-expanded="false" aria-haspopup="true">Articles</button>
|
|
<ul class="dropdown-menu" aria-labelledby="dropdown-articles"><li><a class="dropdown-item" href="../articles/simple-react-app.html">Build a simple ReactJS app</a></li>
|
|
</ul></li>
|
|
<li class="nav-item"><a class="nav-link" href="../news/index.html">Changelog</a></li>
|
|
</ul><ul class="navbar-nav"><li class="nav-item"><form class="form-inline" role="search">
|
|
<input class="form-control" type="search" name="search-input" id="search-input" autocomplete="off" aria-label="Search site" placeholder="Search for" data-search-index="../search.json"></form></li>
|
|
</ul></div>
|
|
|
|
|
|
</div>
|
|
</nav><div class="container template-reference-topic">
|
|
<div class="row">
|
|
<main id="main" class="col-md-9"><div class="page-header">
|
|
|
|
<h1>Types in R and TypeScript</h1>
|
|
|
|
<div class="d-none name"><code>type_objects.Rd</code></div>
|
|
</div>
|
|
|
|
<div class="ref-description section level2">
|
|
<p>This document provides an overview of the main types available to
|
|
app developers, which includes the main types in R and their
|
|
TypeScript counterparts.</p>
|
|
</div>
|
|
|
|
|
|
<div class="section level2">
|
|
<h2 id="ts-objects">TS objects<a class="anchor" aria-label="anchor" href="#ts-objects"></a></h2>
|
|
<p>The basic object in <code>ts</code> is a <code>ts_object</code> class. This is represented by
|
|
input and return types, an optional default value, and a checking
|
|
function.</p>
|
|
<p>Input types describe the <code>zod</code> schema of objects that TypeScript can pass
|
|
to Rserve functions.</p>
|
|
<p>Return types describe the <code>zod</code> schema of objects that Rserve functions,
|
|
and most of these utilise the <code>Robj</code> utility types in the <code>rserve-ts</code>
|
|
library. The return types have additional properties added,
|
|
namedly <code>r_type</code> and <code>r_attributes</code>, handled by the <code>Robj</code> utility types.</p>
|
|
<p><strong>Scalar versus array ("vector") types</strong>:
|
|
In R, almost all types are vectors. In the 'rserve-js' library,
|
|
primitive arrays of length one are converted into scalars, which
|
|
leads to some issues with type checking when, for example, a return value
|
|
has unknown length. <code>which(x > 5)</code> is one such example.</p>
|
|
<p>To solve this, we add an <code>n</code> argument to the <code>ts_*</code> functions. When <code>n = 1</code>,
|
|
the type takes the <em>scalar</em> form of the alue. When <code>n != 1</code>, the type takes
|
|
the <em>array</em> form of the value (this includes 0). Otherwise, the type
|
|
is the union of the scalar and array forms.</p>
|
|
<p>This is the case for numbers, strings, and booleans.</p>
|
|
</div>
|
|
<div class="section level2">
|
|
<h2 id="available-types">Available types<a class="anchor" aria-label="anchor" href="#available-types"></a></h2>
|
|
|
|
<ul><li><p><code>ts_boolean</code>: A boolean value. The array type is <code>Int8Array</code>.</p></li>
|
|
<li><p><code>ts_integer</code>: An integer value. The array type is <code>Int32Array</code>.
|
|
Javascript does not have a native integer type,
|
|
so scalars are represented as a number
|
|
(the same as <code>ts_numeric</code>).</p></li>
|
|
<li><p><code>ts_numeric</code>: A numeric value. The array type is <code>Float64Array</code>.*</p></li>
|
|
<li><p><code>ts_string</code>: A string value. The array type is <code>string[]</code>.</p></li>
|
|
<li><p><code>ts_factor</code>: A factor value. The array type is <code>(level1 | level2 | ... | levelN)[]</code>, and this type does not have a scalar form.</p></li>
|
|
<li><p><code>ts_list</code>: A list value, represented by a named object or an array.</p></li>
|
|
<li><p><code>ts_dataframe</code>: A data frame value, represented by a named object.</p></li>
|
|
<li><p><code>ts_null</code>: A null value.</p></li>
|
|
<li><p><code>ts_void</code>: A void value, used for specifying return types of functions
|
|
that do not return a value.</p></li>
|
|
</ul></div>
|
|
|
|
</main><aside class="col-md-3"><nav id="toc" aria-label="Table of contents"><h2>On this page</h2>
|
|
</nav></aside></div>
|
|
|
|
|
|
<footer><div class="pkgdown-footer-left">
|
|
<p>Developed by Tom Elliott.</p>
|
|
</div>
|
|
|
|
<div class="pkgdown-footer-right">
|
|
<p>Site built with <a href="https://pkgdown.r-lib.org/" class="external-link">pkgdown</a> 2.1.1.</p>
|
|
</div>
|
|
|
|
</footer></div>
|
|
|
|
|
|
|
|
|
|
|
|
</body></html>
|
|
|