Type experimentation

This commit is contained in:
Steve Ruiz 2021-05-25 12:16:01 +01:00
parent 71b8ac94af
commit 638b1b3494
4 changed files with 129 additions and 12 deletions

View 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
}

View file

@ -1,11 +1,11 @@
import { v4 as uuid } from "uuid"
import * as vec from "utils/vec"
import { CircleShape, ShapeType, Corner, Edge } from "types"
import shapeUtilityMap, { registerShapeUtils } from "./index"
import { CircleShape, ShapeType } from "types"
import { registerShapeUtils } from "./index"
import { boundsContained } from "utils/bounds"
import { intersectCircleBounds } from "utils/intersections"
import { pointInCircle } from "utils/hitTests"
import { getTransformAnchor, translateBounds } from "utils/utils"
import { translateBounds } from "utils/utils"
const circle = registerShapeUtils<CircleShape>({
boundsCache: new WeakMap([]),

1
lib/shape-utils/notes.md Normal file
View file

@ -0,0 +1 @@
# Shape Utils

View file

@ -106,7 +106,7 @@ export interface RectangleShape extends BaseShape {
size: number[]
}
export type Shape = Readonly<
export type MutableShape =
| DotShape
| CircleShape
| EllipseShape
@ -114,16 +114,17 @@ export type Shape = Readonly<
| RayShape
| PolylineShape
| RectangleShape
>
export type Shape = Readonly<MutableShape>
export interface Shapes {
[ShapeType.Dot]: DotShape
[ShapeType.Circle]: CircleShape
[ShapeType.Ellipse]: EllipseShape
[ShapeType.Line]: LineShape
[ShapeType.Ray]: RayShape
[ShapeType.Polyline]: PolylineShape
[ShapeType.Rectangle]: RectangleShape
[ShapeType.Dot]: Readonly<DotShape>
[ShapeType.Circle]: Readonly<CircleShape>
[ShapeType.Ellipse]: Readonly<EllipseShape>
[ShapeType.Line]: Readonly<LineShape>
[ShapeType.Ray]: Readonly<RayShape>
[ShapeType.Polyline]: Readonly<PolylineShape>
[ShapeType.Rectangle]: Readonly<RectangleShape>
}
export type ShapeByType<T extends ShapeType> = Shapes[T]