Importing types
Useimport type for type-only imports to ensure they are erased at compile time:
The DType type
DType is a string union representing all supported data types:
NDArray vs NDArrayCore
The two array types correspond to the two main entry points:NDArray extends NDArrayCore, so every NDArray satisfies the NDArrayCore type. Both types expose the same properties: shape, ndim, size, dtype, data, strides, flags, base, itemsize, nbytes.
Writing generic functions
Accept NDArrayCore for maximum compatibility
If your function only uses standalone functions (not method chaining), acceptNDArrayCore. This lets callers pass either type:
Accept NDArray when you need methods
If your function uses method chaining, requireNDArray:
Pattern for library authors
If you are publishing a library that depends on numpy-ts, acceptNDArrayCore in your public API and return NDArrayCore. This gives your users the choice of which entry point to use.
Type narrowing with dtype
Thedtype property is typed as string on the array class. When you need to branch on dtype, narrow it to DType:
isIntegerDType, isFloatDType, and isComplexDType:
Shape inference
Array shapes are exposed asreadonly number[]. You can use this to write shape-aware utilities:
Combining with generics
For functions that work with multiple array types, use TypeScript generics:Since
NDArray extends NDArrayCore, a generic bounded by NDArrayCore accepts both types. The return type preserves the caller’s type through the generic parameter.Type-safe creation functions
All creation functions accept adtype option: