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

Add ICoordinate2D, IVector2D, IMatrix2D #18

Open
lysz210 opened this issue Sep 9, 2019 · 0 comments
Open

Add ICoordinate2D, IVector2D, IMatrix2D #18

lysz210 opened this issue Sep 9, 2019 · 0 comments

Comments

@lysz210
Copy link
Contributor

lysz210 commented Sep 9, 2019

Declaring basic Interfaces with only properties can help passing plain object to method.

interface ICoordinate2D { x:number; y:number }
class Point2D extends ICoordinate2D {
...
  add(that: ICoordinate2D)
...
}

In the example add can be called with a plain object that has an x and y properties that are numbers.
with add(that: Point2D) the method must be called with a Point2D or an object that has x and y as number and all the methods in Point2D class and you're forced to call add(new Point(1,1)) instead of add({x: 1, y: 1}).
A practical use-case with object destructuring:

const el = document.querySelector('#the-div')
const myPoint = new Point()
el.addEventListener('mousemove', ({clientX: x = 0, clientY: y = 0}) => {
  myPoint = myPoint.add({x, y})
})

The IVector2D can be a alias of ICoordinate2D this can allow to:

interface IVector2D extends ICoordinate2D {}
class Vector2D extends IVector2D {
...
}
// this with allow to use Point2D with a Vector
// I'm not sure about this, if can be considered correct.
Vector2D v = new Vector2D(1,2)
Point2D p = new Point2D(1,1)
Point2D p2 = p.add(v) // this doesn't rais any warnings/errors becaus Vector2D is subclass of Vector2D and has numeric properties x and y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant