Bläddra i källkod

Type experimentation

main
Steve Ruiz 4 år sedan
förälder
incheckning
638b1b3494
4 ändrade filer med 129 tillägg och 12 borttagningar
  1. 115
    0
      lib/shape-utils/ShapeTest.tsx
  2. 3
    3
      lib/shape-utils/circle.tsx
  3. 1
    0
      lib/shape-utils/notes.md
  4. 10
    9
      types.ts

+ 115
- 0
lib/shape-utils/ShapeTest.tsx Visa fil

@@ -0,0 +1,115 @@
1
+interface Props {
2
+  name: string
3
+}
4
+
5
+interface Core {
6
+  id: string
7
+}
8
+
9
+interface Instance extends Props, Core {}
10
+
11
+const defaults: Props = {
12
+  name: "Spot",
13
+}
14
+
15
+const core: Core = {
16
+  id: "0",
17
+}
18
+
19
+class ClassInstance<T extends object = {}> implements Instance {
20
+  id = "0"
21
+  name = "Spot"
22
+
23
+  constructor(
24
+    props: Partial<Props> &
25
+      { [K in keyof T]: K extends keyof Core ? never : T[K] }
26
+  ) {
27
+    Object.assign(this, props)
28
+  }
29
+}
30
+
31
+interface InstanceConstructor {
32
+  new <T extends object = {}>(
33
+    props: Partial<Props> &
34
+      { [K in keyof T]: K extends keyof Core ? never : T[K] }
35
+  ): Instance
36
+}
37
+
38
+function makeInstance<T extends object = {}>(
39
+  props: Partial<Props> &
40
+    { [K in keyof T]: K extends keyof Core ? never : T[K] } &
41
+    ThisType<ClassInstance>
42
+) {
43
+  return new ClassInstance<T>({ ...defaults, ...props, ...core })
44
+}
45
+
46
+function getInstance<T extends object = {}>(
47
+  props: Partial<Props> &
48
+    { [K in keyof T]: K extends keyof Core ? never : T[K] }
49
+) {
50
+  return { ...defaults, ...props, ...core }
51
+}
52
+
53
+const instance = getInstance({
54
+  name: "Steve",
55
+  age: 93,
56
+  wag(this: Instance) {
57
+    return this.name
58
+  },
59
+})
60
+
61
+interface AnimalProps {
62
+  name: string
63
+  greet(this: Animal, name: string): string
64
+}
65
+
66
+interface AnimalCore {
67
+  id: string
68
+  sleep(this: Animal): void
69
+}
70
+
71
+interface Animal extends AnimalProps, AnimalCore {}
72
+
73
+const getAnimal = <T extends object>(
74
+  props: Partial<AnimalProps> &
75
+    { [K in keyof T]: K extends keyof AnimalCore ? never : T[K] }
76
+): Animal & T => {
77
+  return {
78
+    // Defaults
79
+    name: "Animal",
80
+    greet(name) {
81
+      return "Hey " + name
82
+    },
83
+    // Overrides
84
+    ...props,
85
+    // Core
86
+    id: "hi",
87
+    sleep() {},
88
+  }
89
+}
90
+
91
+const dog = getAnimal({
92
+  name: "doggo",
93
+  greet(name) {
94
+    return "Woof " + this.name
95
+  },
96
+  wag() {
97
+    return "wagging..."
98
+  },
99
+})
100
+
101
+dog.greet("steve")
102
+dog.wag()
103
+dog.sleep()
104
+
105
+class ShapeTest {}
106
+
107
+const shapeTest = new ShapeTest()
108
+
109
+export default shapeTest
110
+
111
+type Greet = (name: string) => string
112
+
113
+const greet: Greet = (name: string | number) => {
114
+  return "hello " + name
115
+}

+ 3
- 3
lib/shape-utils/circle.tsx Visa fil

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

+ 1
- 0
lib/shape-utils/notes.md Visa fil

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

+ 10
- 9
types.ts Visa fil

@@ -106,7 +106,7 @@ export interface RectangleShape extends BaseShape {
106 106
   size: number[]
107 107
 }
108 108
 
109
-export type Shape = Readonly<
109
+export type MutableShape =
110 110
   | DotShape
111 111
   | CircleShape
112 112
   | EllipseShape
@@ -114,16 +114,17 @@ export type Shape = Readonly<
114 114
   | RayShape
115 115
   | PolylineShape
116 116
   | RectangleShape
117
->
117
+
118
+export type Shape = Readonly<MutableShape>
118 119
 
119 120
 export interface Shapes {
120
-  [ShapeType.Dot]: DotShape
121
-  [ShapeType.Circle]: CircleShape
122
-  [ShapeType.Ellipse]: EllipseShape
123
-  [ShapeType.Line]: LineShape
124
-  [ShapeType.Ray]: RayShape
125
-  [ShapeType.Polyline]: PolylineShape
126
-  [ShapeType.Rectangle]: RectangleShape
121
+  [ShapeType.Dot]: Readonly<DotShape>
122
+  [ShapeType.Circle]: Readonly<CircleShape>
123
+  [ShapeType.Ellipse]: Readonly<EllipseShape>
124
+  [ShapeType.Line]: Readonly<LineShape>
125
+  [ShapeType.Ray]: Readonly<RayShape>
126
+  [ShapeType.Polyline]: Readonly<PolylineShape>
127
+  [ShapeType.Rectangle]: Readonly<RectangleShape>
127 128
 }
128 129
 
129 130
 export type ShapeByType<T extends ShapeType> = Shapes[T]

Laddar…
Avbryt
Spara