Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Narrow 'multiply' return types #68

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,12 @@ describe("Matrix.multiply", () => {
describe("Matrix#multiply", () => {
test("multiply throws typerror", () => {
expect(
Matrix.multiply.bind(
Matrix.multiply.bind<
typeof Matrix,
[x: Matrix1D, y: Matrix2D],
[],
Matrix1D
>(
Matrix,
Matrix([1, 2, 3]),
Matrix([
Expand Down
24 changes: 18 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Matrix.addable = function (x: Matrix, y: Matrix): boolean {
*/
function Add(x: Matrix1D, y: Matrix1D): Matrix1D;
function Add(x: Matrix2D, y: Matrix2D): Matrix2D;
function Add<T extends Matrix1D & Matrix2D>(x: T, y: T): Matrix1D | Matrix2D {
function Add<T extends Matrix1D | Matrix2D>(x: T, y: T): Matrix1D | Matrix2D {
if (!Matrix.addable(x, y)) throw new TypeError("Matrices are not addable");
return x.map((row: number[], i: number): number[] =>
row.map((column: number, j: number): number => {
Expand Down Expand Up @@ -134,7 +134,12 @@ function innerproduct(x: Matrix1D, y: Matrix, i: number): number {
* @param {Matrix} y - Matrix to multiply
* @return {Matrix} New matrix with the dot product
*/
Matrix.multiply = function (x: Matrix, y: Matrix): Matrix {
function Multiply(x: Matrix1D, y: Matrix): Matrix1D;
function Multiply(x: Matrix2D, y: Matrix): Matrix2D;
function Multiply<T extends Matrix1D | Matrix2D>(
x: T,
y: Matrix
): Matrix1D | Matrix2D {
if (!Matrix.multipliable(x, y)) {
throw new TypeError("Matrices are not multipliable");
}
Expand All @@ -154,7 +159,8 @@ Matrix.multiply = function (x: Matrix, y: Matrix): Matrix {
}
});
}
};
}
Matrix.multiply = Multiply;

/**
* Inverts a matrix. Matrix must be a square (e.g. 1x1 or 2x2)
Expand All @@ -164,7 +170,7 @@ Matrix.multiply = function (x: Matrix, y: Matrix): Matrix {
*/
function Invert(x: Matrix1D): Matrix1D;
function Invert(x: Matrix2D): Matrix2D;
function Invert<T extends Matrix1D & Matrix2D>(x: T): Matrix1D | Matrix2D {
function Invert<T extends Matrix1D | Matrix2D>(x: T): Matrix1D | Matrix2D {
return Matrix(inv<any>(x.__value));
}
Matrix.invert = Invert;
Expand Down Expand Up @@ -231,9 +237,15 @@ Matrix.prototype.multipliable = function (this: Matrix, y: Matrix): boolean {
* @param {Matrix} y - Matrix to multiply
* @return {Matrix} New matrix with the dot product
*/
Matrix.prototype.multiply = function (this: Matrix, y: Matrix): Matrix {
function multiply(this: Matrix1D, y: Matrix): Matrix1D;
function multiply(this: Matrix2D, y: Matrix): Matrix2D;
function multiply<T extends Matrix1D & Matrix2D>(
this: T,
y: Matrix
): Matrix1D | Matrix2D {
return Matrix.multiply(this, y);
};
}
Matrix.prototype.multiply = multiply;

/**
* Calculates the transpose of this matrix
Expand Down