Skip to content

Commit

Permalink
fix(typings): improve generator types (#190)
Browse files Browse the repository at this point in the history
Previously, code like this would fail to typecheck:
```ts
import {Matrix} from "ml-matrix"
let x = [...Matrix.eye(5).values()]
```
Typescript would give the error:
> Cannot iterate value because the 'next' method of its iterator expects type 'never', but array spread will always send 'undefined'.(2764)
  • Loading branch information
rotu authored Oct 14, 2024
1 parent f58cd6b commit ee83c8b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
12 changes: 6 additions & 6 deletions matrix.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -853,8 +853,8 @@ export abstract class AbstractMatrix {
[Symbol.iterator](): Generator<
[row: number, column: number, value: number],
void,
never
>;
void
>;

/**
* iterator from left to right, from top to bottom
Expand All @@ -863,14 +863,14 @@ export abstract class AbstractMatrix {
entries(): Generator<
[row: number, column: number, value: number],
void,
never
void
>;

/**
* iterator from left to right, from top to bottom
* yield value
*/
values(): Generator<number, void, never>;
values(): Generator<number, void, void>;

// From here we document methods dynamically generated from operators

Expand Down Expand Up @@ -1145,14 +1145,14 @@ export class SymmetricMatrix extends AbstractMatrix {
upperRightEntries(): Generator<
[row: number, column: number, value: number],
void,
never
void
>;

/**
* half iterator upper-right-corner from left to right, from top to bottom
* yield value
*/
upperRightValues(): Generator<number, void, never>;
upperRightValues(): Generator<number, void, void>;
}

export class DistanceMatrix extends SymmetricMatrix {
Expand Down
4 changes: 2 additions & 2 deletions src/matrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -1530,7 +1530,7 @@ export class AbstractMatrix {
/**
* iterator from left to right, from top to bottom
* yield [row, column, value]
* @returns {Generator<[number, number, number], void, *>}
* @returns {Generator<[number, number, number], void, void>}
*/
*entries() {
for (let row = 0; row < this.rows; row++) {
Expand All @@ -1543,7 +1543,7 @@ export class AbstractMatrix {
/**
* iterator from left to right, from top to bottom
* yield value
* @returns {Generator<number, void, *>}
* @returns {Generator<number, void, void>}
*/
*values() {
for (let row = 0; row < this.rows; row++) {
Expand Down
4 changes: 2 additions & 2 deletions src/symmetricMatrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ export class SymmetricMatrix extends AbstractMatrix {
* half iterator upper-right-corner from left to right, from top to bottom
* yield [row, column, value]
*
* @returns {Generator<[number, number, number], void, *>}
* @returns {Generator<[number, number, number], void, void>}
*/
*upperRightEntries() {
for (let row = 0, col = 0; row < this.diagonalSize; void 0) {
Expand All @@ -232,7 +232,7 @@ export class SymmetricMatrix extends AbstractMatrix {
* half iterator upper-right-corner from left to right, from top to bottom
* yield value
*
* @returns {Generator<[number, number, number], void, *>}
* @returns {Generator<[number, number, number], void, void>}
*/
*upperRightValues() {
for (let row = 0, col = 0; row < this.diagonalSize; void 0) {
Expand Down

0 comments on commit ee83c8b

Please sign in to comment.