Importing
Creating arrays
Arithmetic
numpy-ts does not overload JavaScript operators. Use
np.add(a, b) or a.add(b) instead of a + b. Method chaining (.add(), .reshape(), etc.) is available on NDArray from the full entry point. See Tree-Shaking & Bundle Size for details on the two entry points.Indexing and slicing
This is the biggest syntax change. Python uses bracket notation with colons; numpy-ts uses a.slice() method with string arguments.
Slicing syntax reference
| Python | numpy-ts | Description |
|---|---|---|
a[i] | a.item(i) (scalar) or a.slice('i', ':') (row) | Single index |
a[i, j] | a.item(i, j) | Single element |
a[0:5] | a.slice('0:5') | Range |
a[::2] | a.slice('::2') | Every other element |
a[::-1] | a.slice('::-1') | Reverse |
a[0:5, :] | a.slice('0:5', ':') | Multi-axis slice |
a[:, 1:3] | a.slice(':', '1:3') | Column slice |
Reshaping
Reductions
Linear algebra
Random
As of v1.2.0,
numpy-ts uses the same RNGs as NumPy (MT19937 for the legacy API, PCG64 for the modern API). Random outputs now match NumPy exactly for all distributions given the same seed.FFT
File I/O
Common gotchas
| Gotcha | Python (NumPy) | TypeScript (numpy-ts) | Notes |
|---|---|---|---|
| Operator overloading | a + b, a * b | np.add(a, b) or a.add(b) | JS does not support operator overloading |
| Shape arguments | reshape(3, 3) or reshape((3, 3)) | reshape([3, 3]) | Always use an array, never separate args |
| Indexing | a[0:5, :] | a.slice('0:5', ':') | String-based slicing |
| Keyword arguments | axis=0, keepdims=True | Positional or options object | JS has no keyword args |
var is reserved | np.var(a) | np.variance(a) | var is a JS reserved word; np.var is also aliased but may conflict |
| Transpose | a.T | a.T | Same syntax — .T is a getter property |
| Array equality | a == b (element-wise) | np.equal(a, b) or np.array_equal(a, b) | == compares object references in JS |
| Tuple axes | axis=(0, 2) | axis=[0, 2] | Use arrays instead of tuples |
| int64/uint64 | Regular integers | BigInt values | TypedArray requirement; np.array([1n, 2n, 3n], 'int64') |
| In-place ops | a += b | Not supported | numpy-ts operations always return new arrays |
| dtype specification | dtype=np.float32 | 'float32' (string) | Dtypes are string literals in numpy-ts |
Supported dtypes
numpy-ts supports 14 data types that map to JavaScript TypedArrays:| Category | Dtypes | JS Storage | Notes |
|---|---|---|---|
| Floating point | float64 (default), float32, float16 | Float64Array, Float32Array, Float16Array | float16 uses Float32Array fallback on older runtimes |
| Signed integer | int8, int16, int32, int64 | Int8Array … BigInt64Array | int64 uses BigInt |
| Unsigned integer | uint8, uint16, uint32, uint64 | Uint8Array … BigUint64Array | uint64 uses BigInt |
| Boolean | bool | Uint8Array | Stored as 0/1 |
| Complex | complex64, complex128 | Interleaved Float32Array/Float64Array | Complex class for elements |
What about missing NumPy features?
numpy-ts covers 476 of 507 NumPy functions (94%). A few categories have partial coverage:- Structured arrays / record dtypes — not supported (JS has no equivalent)
- String operations (
np.char) — not supported (use native JS string methods) np.ma(masked arrays) — not supported yetnp.matrix— deprecated in NumPy itself; use 2DNDArrayinstead
Next steps
Quickstart
Hands-on tutorial covering all major features.
API Reference
Complete function reference organized by category.
Tree-Shaking
Full library vs core: which entry point is right for you?