Skip to content

Commit ffc88e5

Browse files
authored
fix: Narrow 'multiply' return types (#68)
2 parents 80aa5de + cd5e587 commit ffc88e5

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

src/__tests__/index.test.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,12 @@ describe("Matrix.multiply", () => {
332332
describe("Matrix#multiply", () => {
333333
test("multiply throws typerror", () => {
334334
expect(
335-
Matrix.multiply.bind(
335+
Matrix.multiply.bind<
336+
typeof Matrix,
337+
[x: Matrix1D, y: Matrix2D],
338+
[],
339+
Matrix1D
340+
>(
336341
Matrix,
337342
Matrix([1, 2, 3]),
338343
Matrix([

src/index.ts

+18-6
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ Matrix.addable = function (x: Matrix, y: Matrix): boolean {
8585
*/
8686
function Add(x: Matrix1D, y: Matrix1D): Matrix1D;
8787
function Add(x: Matrix2D, y: Matrix2D): Matrix2D;
88-
function Add<T extends Matrix1D & Matrix2D>(x: T, y: T): Matrix1D | Matrix2D {
88+
function Add<T extends Matrix1D | Matrix2D>(x: T, y: T): Matrix1D | Matrix2D {
8989
if (!Matrix.addable(x, y)) throw new TypeError("Matrices are not addable");
9090
return x.map((row: number[], i: number): number[] =>
9191
row.map((column: number, j: number): number => {
@@ -134,7 +134,12 @@ function innerproduct(x: Matrix1D, y: Matrix, i: number): number {
134134
* @param {Matrix} y - Matrix to multiply
135135
* @return {Matrix} New matrix with the dot product
136136
*/
137-
Matrix.multiply = function (x: Matrix, y: Matrix): Matrix {
137+
function Multiply(x: Matrix1D, y: Matrix): Matrix1D;
138+
function Multiply(x: Matrix2D, y: Matrix): Matrix2D;
139+
function Multiply<T extends Matrix1D | Matrix2D>(
140+
x: T,
141+
y: Matrix
142+
): Matrix1D | Matrix2D {
138143
if (!Matrix.multipliable(x, y)) {
139144
throw new TypeError("Matrices are not multipliable");
140145
}
@@ -154,7 +159,8 @@ Matrix.multiply = function (x: Matrix, y: Matrix): Matrix {
154159
}
155160
});
156161
}
157-
};
162+
}
163+
Matrix.multiply = Multiply;
158164

159165
/**
160166
* Inverts a matrix. Matrix must be a square (e.g. 1x1 or 2x2)
@@ -164,7 +170,7 @@ Matrix.multiply = function (x: Matrix, y: Matrix): Matrix {
164170
*/
165171
function Invert(x: Matrix1D): Matrix1D;
166172
function Invert(x: Matrix2D): Matrix2D;
167-
function Invert<T extends Matrix1D & Matrix2D>(x: T): Matrix1D | Matrix2D {
173+
function Invert<T extends Matrix1D | Matrix2D>(x: T): Matrix1D | Matrix2D {
168174
return Matrix(inv<any>(x.__value));
169175
}
170176
Matrix.invert = Invert;
@@ -231,9 +237,15 @@ Matrix.prototype.multipliable = function (this: Matrix, y: Matrix): boolean {
231237
* @param {Matrix} y - Matrix to multiply
232238
* @return {Matrix} New matrix with the dot product
233239
*/
234-
Matrix.prototype.multiply = function (this: Matrix, y: Matrix): Matrix {
240+
function multiply(this: Matrix1D, y: Matrix): Matrix1D;
241+
function multiply(this: Matrix2D, y: Matrix): Matrix2D;
242+
function multiply<T extends Matrix1D & Matrix2D>(
243+
this: T,
244+
y: Matrix
245+
): Matrix1D | Matrix2D {
235246
return Matrix.multiply(this, y);
236-
};
247+
}
248+
Matrix.prototype.multiply = multiply;
237249

238250
/**
239251
* Calculates the transpose of this matrix

0 commit comments

Comments
 (0)