From 8df54886bfad31ac725d46583e1f58d0a44df556 Mon Sep 17 00:00:00 2001 From: Abbe Keultjes Date: Fri, 12 Feb 2021 15:54:34 +0100 Subject: [PATCH] fix(hex): when overriding a hex prototype method, `this` is now correctly typed _Polymorphic this_ was the magic word. Thanks to jcalz on stackoverflow: https://stackoverflow.com/questions/66162731/how-to-set-this-in-extended-interface-method-to-type-variable?noredirect=1#comment116992054_66162731 --- src/hex/functions/createHexPrototype.ts | 6 ++---- src/hex/types.ts | 4 ++-- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/hex/functions/createHexPrototype.ts b/src/hex/functions/createHexPrototype.ts index 4aec1a27..a636e87f 100644 --- a/src/hex/functions/createHexPrototype.ts +++ b/src/hex/functions/createHexPrototype.ts @@ -81,15 +81,13 @@ const assertOffset = ({ offset }: HexPrototypeOptions) => { return offset } -export const createHexPrototype = ( - customPrototype?: T | Partial, -) => { +export const createHexPrototype = (customPrototype?: T | Partial) => { // pseudo private property const s = new WeakMap() const prototype = { ...defaultHexSettings, - copy(newProps = {}) { + copy(newProps) { return copyHex(this, newProps) }, // fixme: make this a getter and name it `asPoint`, or better: add getters for x and y diff --git a/src/hex/types.ts b/src/hex/types.ts index 2c1753fa..07a4372e 100644 --- a/src/hex/types.ts +++ b/src/hex/types.ts @@ -61,8 +61,8 @@ export interface DefaultHexPrototype extends HexSettings { s: number // todo: about 80% sure the newProps type works (it's used in more places, if it works: maybe make it a separate type?) - copy(this: T, newProps?: Partial | HexCoordinates): T - toPoint(this: T): Point + copy(this: this, newProps?: Partial | HexCoordinates): this + toPoint(this: this): Point } export interface Hex extends DefaultHexPrototype, AxialCoordinates {}