Skip to content

Commit

Permalink
automatic typing of the returned array
Browse files Browse the repository at this point in the history
  • Loading branch information
mmomtchev committed Nov 30, 2023
1 parent 954a9d1 commit 3f24200
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.5.0] 2023-11-30
- When using TypeScript, support automatic typing of the returned array through generics

## [1.4.0] 2023-09-21
- Update all dependencies and test with recent Node.js versions

Expand Down
20 changes: 10 additions & 10 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import * as gdal from 'gdal-async';

declare module 'gdal-async' {

export type ArrayOptions = {
data?: ndarray.NdArray<TypedArray>|stdlib.ndarray;
export type ArrayOptions<T extends TypedArray = TypedArray> = {
data?: ndarray.NdArray<T>|stdlib.ndarray;
x?: number;
y?: number;
width?: number;
Expand All @@ -16,8 +16,8 @@ export type ArrayOptions = {
progress_cb?: ProgressCb;
}

export type NDArrayOptions = {
data?: ndarray.NdArray<TypedArray>|stdlib.ndarray;
export type NDArrayOptions<T extends TypedArray = TypedArray> = {
data?: ndarray.NdArray<T>|stdlib.ndarray;
origin?: number[];
span?: number[];
}
Expand Down Expand Up @@ -47,7 +47,7 @@ export interface MDArray {
* @throws {Error}
* @returns {ndarray.NdArray<TypedArray>}
*/
readArray(options?: NDArrayOptions): ndarray.NdArray<TypedArray>
readArray<T extends TypedArray = TypedArray>(options?: NDArrayOptions): ndarray.NdArray<T>

/**
* Read the selection region into the given ndarray or a new ndarray, async version.
Expand All @@ -65,7 +65,7 @@ export interface MDArray {
* @param {number[]} [options.span] Full size if not specified
* @returns {Promise<ndarray.NdArray<TypedArray>>}
*/
readArrayAsync(options?: NDArrayOptions): Promise<ndarray.NdArray<TypedArray>>
readArrayAsync<T extends TypedArray = TypedArray>(options?: NDArrayOptions): Promise<ndarray.NdArray<T>>
}

export interface RasterBandPixels {
Expand All @@ -89,7 +89,7 @@ export interface RasterBandPixels {
*
* @method readArray
* @param {ArrayOptions} [options]
* @param {ndarray.NdArray<TypedArray>} [options.data]
* @param {ndarray.NdArray<T extends TypedArray = TypedArray>} [options.data]
* @param {number} [options.x]
* @param {number} [options.y]
* @param {number} [options.width]
Expand All @@ -99,7 +99,7 @@ export interface RasterBandPixels {
* @throws {Error}
* @returns {ndarray.NdArray<TypedArray>}
*/
readArray(options?: ArrayOptions): ndarray.NdArray<TypedArray>
readArray<T extends TypedArray = TypedArray>(options?: ArrayOptions<T>): ndarray.NdArray<T>

/**
* Read the selection region into the given ndarray or a new ndarray, async version.
Expand All @@ -114,7 +114,7 @@ export interface RasterBandPixels {
*
* @method readArrayAsync
* @param {ArrayOptions} [options]
* @param {ndarray.NdArray<TypedArray>} [options.data]
* @param {ndarray.NdArray<T extends TypedArray = TypedArray>} [options.data]
* @param {number} [options.x]
* @param {number} [options.y]
* @param {number} [options.width]
Expand All @@ -123,7 +123,7 @@ export interface RasterBandPixels {
* @param {ProgressCb} [options.progress_cb]
* @returns {Promise<ndarray.NdArray<TypedArray>>}
*/
readArrayAsync(options?: ArrayOptions): Promise<ndarray.NdArray<TypedArray>>
readArrayAsync<T extends TypedArray = TypedArray>(options?: ArrayOptions<T>): Promise<ndarray.NdArray<T>>

/**
* Write the selection region from the given ndarray.
Expand Down
2 changes: 1 addition & 1 deletion test/ndarray-gdal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ describe('ndarray-gdal TS', () => {

describe('gdal.MDArray', () => {
describe('readArray', () => {
let original: ndarray.NdArray;
let original: ndarray.NdArray<Float32Array>;
let ds: gdal.Dataset, array: gdal.MDArray;

before(() => {
Expand Down

0 comments on commit 3f24200

Please sign in to comment.