Type experimentation
This commit is contained in:
parent
71b8ac94af
commit
638b1b3494
4 changed files with 129 additions and 12 deletions
115
lib/shape-utils/ShapeTest.tsx
Normal file
115
lib/shape-utils/ShapeTest.tsx
Normal file
|
@ -0,0 +1,115 @@
|
||||||
|
interface Props {
|
||||||
|
name: string
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Core {
|
||||||
|
id: string
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Instance extends Props, Core {}
|
||||||
|
|
||||||
|
const defaults: Props = {
|
||||||
|
name: "Spot",
|
||||||
|
}
|
||||||
|
|
||||||
|
const core: Core = {
|
||||||
|
id: "0",
|
||||||
|
}
|
||||||
|
|
||||||
|
class ClassInstance<T extends object = {}> implements Instance {
|
||||||
|
id = "0"
|
||||||
|
name = "Spot"
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
props: Partial<Props> &
|
||||||
|
{ [K in keyof T]: K extends keyof Core ? never : T[K] }
|
||||||
|
) {
|
||||||
|
Object.assign(this, props)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
interface InstanceConstructor {
|
||||||
|
new <T extends object = {}>(
|
||||||
|
props: Partial<Props> &
|
||||||
|
{ [K in keyof T]: K extends keyof Core ? never : T[K] }
|
||||||
|
): Instance
|
||||||
|
}
|
||||||
|
|
||||||
|
function makeInstance<T extends object = {}>(
|
||||||
|
props: Partial<Props> &
|
||||||
|
{ [K in keyof T]: K extends keyof Core ? never : T[K] } &
|
||||||
|
ThisType<ClassInstance>
|
||||||
|
) {
|
||||||
|
return new ClassInstance<T>({ ...defaults, ...props, ...core })
|
||||||
|
}
|
||||||
|
|
||||||
|
function getInstance<T extends object = {}>(
|
||||||
|
props: Partial<Props> &
|
||||||
|
{ [K in keyof T]: K extends keyof Core ? never : T[K] }
|
||||||
|
) {
|
||||||
|
return { ...defaults, ...props, ...core }
|
||||||
|
}
|
||||||
|
|
||||||
|
const instance = getInstance({
|
||||||
|
name: "Steve",
|
||||||
|
age: 93,
|
||||||
|
wag(this: Instance) {
|
||||||
|
return this.name
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
interface AnimalProps {
|
||||||
|
name: string
|
||||||
|
greet(this: Animal, name: string): string
|
||||||
|
}
|
||||||
|
|
||||||
|
interface AnimalCore {
|
||||||
|
id: string
|
||||||
|
sleep(this: Animal): void
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Animal extends AnimalProps, AnimalCore {}
|
||||||
|
|
||||||
|
const getAnimal = <T extends object>(
|
||||||
|
props: Partial<AnimalProps> &
|
||||||
|
{ [K in keyof T]: K extends keyof AnimalCore ? never : T[K] }
|
||||||
|
): Animal & T => {
|
||||||
|
return {
|
||||||
|
// Defaults
|
||||||
|
name: "Animal",
|
||||||
|
greet(name) {
|
||||||
|
return "Hey " + name
|
||||||
|
},
|
||||||
|
// Overrides
|
||||||
|
...props,
|
||||||
|
// Core
|
||||||
|
id: "hi",
|
||||||
|
sleep() {},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const dog = getAnimal({
|
||||||
|
name: "doggo",
|
||||||
|
greet(name) {
|
||||||
|
return "Woof " + this.name
|
||||||
|
},
|
||||||
|
wag() {
|
||||||
|
return "wagging..."
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
dog.greet("steve")
|
||||||
|
dog.wag()
|
||||||
|
dog.sleep()
|
||||||
|
|
||||||
|
class ShapeTest {}
|
||||||
|
|
||||||
|
const shapeTest = new ShapeTest()
|
||||||
|
|
||||||
|
export default shapeTest
|
||||||
|
|
||||||
|
type Greet = (name: string) => string
|
||||||
|
|
||||||
|
const greet: Greet = (name: string | number) => {
|
||||||
|
return "hello " + name
|
||||||
|
}
|
|
@ -1,11 +1,11 @@
|
||||||
import { v4 as uuid } from "uuid"
|
import { v4 as uuid } from "uuid"
|
||||||
import * as vec from "utils/vec"
|
import * as vec from "utils/vec"
|
||||||
import { CircleShape, ShapeType, Corner, Edge } from "types"
|
import { CircleShape, ShapeType } from "types"
|
||||||
import shapeUtilityMap, { registerShapeUtils } from "./index"
|
import { registerShapeUtils } from "./index"
|
||||||
import { boundsContained } from "utils/bounds"
|
import { boundsContained } from "utils/bounds"
|
||||||
import { intersectCircleBounds } from "utils/intersections"
|
import { intersectCircleBounds } from "utils/intersections"
|
||||||
import { pointInCircle } from "utils/hitTests"
|
import { pointInCircle } from "utils/hitTests"
|
||||||
import { getTransformAnchor, translateBounds } from "utils/utils"
|
import { translateBounds } from "utils/utils"
|
||||||
|
|
||||||
const circle = registerShapeUtils<CircleShape>({
|
const circle = registerShapeUtils<CircleShape>({
|
||||||
boundsCache: new WeakMap([]),
|
boundsCache: new WeakMap([]),
|
||||||
|
|
1
lib/shape-utils/notes.md
Normal file
1
lib/shape-utils/notes.md
Normal file
|
@ -0,0 +1 @@
|
||||||
|
# Shape Utils
|
19
types.ts
19
types.ts
|
@ -106,7 +106,7 @@ export interface RectangleShape extends BaseShape {
|
||||||
size: number[]
|
size: number[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Shape = Readonly<
|
export type MutableShape =
|
||||||
| DotShape
|
| DotShape
|
||||||
| CircleShape
|
| CircleShape
|
||||||
| EllipseShape
|
| EllipseShape
|
||||||
|
@ -114,16 +114,17 @@ export type Shape = Readonly<
|
||||||
| RayShape
|
| RayShape
|
||||||
| PolylineShape
|
| PolylineShape
|
||||||
| RectangleShape
|
| RectangleShape
|
||||||
>
|
|
||||||
|
export type Shape = Readonly<MutableShape>
|
||||||
|
|
||||||
export interface Shapes {
|
export interface Shapes {
|
||||||
[ShapeType.Dot]: DotShape
|
[ShapeType.Dot]: Readonly<DotShape>
|
||||||
[ShapeType.Circle]: CircleShape
|
[ShapeType.Circle]: Readonly<CircleShape>
|
||||||
[ShapeType.Ellipse]: EllipseShape
|
[ShapeType.Ellipse]: Readonly<EllipseShape>
|
||||||
[ShapeType.Line]: LineShape
|
[ShapeType.Line]: Readonly<LineShape>
|
||||||
[ShapeType.Ray]: RayShape
|
[ShapeType.Ray]: Readonly<RayShape>
|
||||||
[ShapeType.Polyline]: PolylineShape
|
[ShapeType.Polyline]: Readonly<PolylineShape>
|
||||||
[ShapeType.Rectangle]: RectangleShape
|
[ShapeType.Rectangle]: Readonly<RectangleShape>
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ShapeByType<T extends ShapeType> = Shapes[T]
|
export type ShapeByType<T extends ShapeType> = Shapes[T]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue