Skip to content

Commit 2459f86

Browse files
author
Rayat Rahman
committed
chore(types): Add grok files for TS PR questions/docs
1 parent 2057c83 commit 2459f86

4 files changed

+341
-0
lines changed
Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
{
2+
"cells": [
3+
{
4+
"blockId": "10ecf944-1813-4dad-813a-4e3e3a9ce05e",
5+
"data": {
6+
"formatCode": false,
7+
"isPreviewing": true,
8+
"text": "This function's return type is tricky.<div><br></div><div>\r<div>If in IE11 or other environments where window/globalThis does not contain object properties for each type of DOM Event Class,\r <span style=\"color: var(--text-color); font-family: var(--font-family); font-size: var(--font-size); font-weight: var(--font-weight);\">it returns the value of window.document.createEvent(typeof eventType) &amp; init`. </span></div><div><span style=\"color: var(--text-color); font-family: var(--font-family); font-size: var(--font-size); font-weight: var(--font-weight);\"><br></span></div><div><span style=\"color: var(--text-color); font-family: var(--font-family); font-size: var(--font-size); font-weight: var(--font-weight);\">In all other environments, it simply returns the Event or Event-like object constructed by `new window[typeof eventType]()` </span><span style=\"color: var(--text-color); font-family: var(--font-family); font-size: var(--font-size); font-weight: var(--font-weight);\">I'm not sure how best to represent this in TS. For now, I will specify its return type as if it is invoked in the latter case.</span></div></div>"
9+
},
10+
"id": "10ecf944-1813-4dad-813a-4e3e3a9ce05e",
11+
"type": "markdown"
12+
},
13+
{
14+
"blockId": "22da9304-9908-4580-a3df-c1633c7be0c1",
15+
"data": {
16+
"contents": "File not found",
17+
"file": "src/events.ts",
18+
"range": {
19+
"end": {
20+
"character": 2147483647,
21+
"line": 0
22+
},
23+
"start": {
24+
"character": 0,
25+
"line": 0
26+
}
27+
},
28+
"scm": {
29+
"authors": [],
30+
"branch": "pr/ts/src/events",
31+
"file": "src/events.ts",
32+
"originalRange": {
33+
"end": {
34+
"character": 2147483647,
35+
"line": 173
36+
},
37+
"start": {
38+
"character": 0,
39+
"line": 146
40+
}
41+
},
42+
"remotes": [
43+
{
44+
"name": "origin",
45+
"url": "github.com/riotrah/dom-testing-library"
46+
},
47+
{
48+
"name": "upstream",
49+
"url": "github.com/testing-library/dom-testing-library"
50+
}
51+
],
52+
"repoName": "dom-testing-library",
53+
"revision": "458ef8738cb56de666899737d9b9aa94b32c9153",
54+
"sameRepo": true
55+
}
56+
},
57+
"id": "22da9304-9908-4580-a3df-c1633c7be0c1",
58+
"type": "code",
59+
"version": 1
60+
},
61+
{
62+
"blockId": "3dd1f069-6d23-4bac-856d-2da380292acd",
63+
"data": {
64+
"contents": "File not found",
65+
"file": "src/events.ts",
66+
"range": {
67+
"end": {
68+
"character": 2147483647,
69+
"line": 0
70+
},
71+
"start": {
72+
"character": 0,
73+
"line": 0
74+
}
75+
},
76+
"scm": {
77+
"authors": [],
78+
"branch": "pr/ts/src/events",
79+
"file": "src/events.ts",
80+
"originalRange": {
81+
"end": {
82+
"character": 2147483647,
83+
"line": 207
84+
},
85+
"start": {
86+
"character": 0,
87+
"line": 207
88+
}
89+
},
90+
"remotes": [
91+
{
92+
"name": "origin",
93+
"url": "github.com/riotrah/dom-testing-library"
94+
},
95+
{
96+
"name": "upstream",
97+
"url": "github.com/testing-library/dom-testing-library"
98+
}
99+
],
100+
"repoName": "dom-testing-library",
101+
"revision": "458ef8738cb56de666899737d9b9aa94b32c9153",
102+
"sameRepo": true
103+
}
104+
},
105+
"id": "3dd1f069-6d23-4bac-856d-2da380292acd",
106+
"type": "code",
107+
"version": 1
108+
},
109+
{
110+
"blockId": "603e6503-4d39-42df-a812-7786f5081c1f",
111+
"data": {
112+
"formatCode": false,
113+
"isPreviewing": true,
114+
"text": "<div><span style=\"color: var(--text-color); font-family: var(--font-family); font-size: var(--font-size); font-weight: var(--font-weight);\">The type of `init` is tricky. </span><span style=\"color: var(--text-color); font-family: var(--font-family); font-size: var(--font-size); font-weight: var(--font-weight);\">If called in an environment that exposes an Event-extending class per Event type name in `window` or `globalThis`, </span><span style=\"color: var(--text-color); font-family: var(--font-family); font-size: var(--font-size); font-weight: var(--font-weight);\">its type is that of the corresponding EventInit or EventInit-like interface. \n</span></div><div><span style=\"color: var(--text-color); font-family: var(--font-family); font-size: var(--font-size); font-weight: var(--font-weight);\"><br></span></div><div>\r</div><div>Eg: If:\r</div><div>\r</div><div> 1. Aforemention environmental conditions are met\r </div><div> 2. And eventName extends keyof typeof window (eg eventName === \"MouseEvent\", etc)\r </div><div> 3. Then init will/should be (at least partially) MouseEventInit, as determined by the 2nd argument for the MouseEvent constructor, etc\r</div><div><span style=\"color: var(--text-color); font-family: var(--font-family); font-size: var(--font-size); font-weight: var(--font-weight);\"><br></span></div><div><span style=\"color: var(--text-color); font-family: var(--font-family); font-size: var(--font-size); font-weight: var(--font-weight);\">However, if in an environment (eg IE11) where there does not exist such Event classes in window/globalThis, or if said property is not a Class, </span><span style=\"color: var(--text-color); font-family: var(--font-family); font-size: var(--font-size); font-weight: var(--font-weight);\">`init` can be more correctly regarded as being of type Omit&lt; MyEvent , 'bubbles' | 'cancelable' | 'detail' &gt;, </span><span style=\"color: var(--text-color); font-family: var(--font-family); font-size: var(--font-size); font-weight: var(--font-weight);\">where MyEvent is the actual Event derivative being created. That is, `init` is not so much an EventInit-like object so much as an Event-like one.\n</span></div><div><span style=\"color: var(--text-color); font-family: var(--font-family); font-size: var(--font-size); font-weight: var(--font-weight);\"><br></span></div><div>Technically, whatever `init` is, in the IE11 case, its properties will be merged with the Event that gets created, so it could have any shape,\r <span style=\"color: var(--text-color); font-family: var(--font-family); font-size: var(--font-size); font-weight: var(--font-weight);\">including one that might violate any constraints that assumed the return of this function to be a \"legal\" DOM Event or derivative thereof. </span></div><div><span style=\"color: var(--text-color); font-family: var(--font-family); font-size: var(--font-size); font-weight: var(--font-weight);\"><br></span></div><div><span style=\"color: var(--text-color); font-family: var(--font-family); font-size: var(--font-size); font-weight: var(--font-weight);\">Given that the IE11 case is the edge case, I have defined `init` somewhat broadly, but with preference given to its type as being that of an EventInit-like object.</span></div><div><span style=\"color: var(--text-color); font-family: var(--font-family); font-size: var(--font-size); font-weight: var(--font-weight);\"><br></span></div><div><span style=\"color: var(--text-color); font-family: var(--font-family); font-size: var(--font-size); font-weight: var(--font-weight);\">In all cases, init may optionally define 'dataTransfer' and 'clipboardData' properties as well, </span><span style=\"color: var(--text-color); font-family: var(--font-family); font-size: var(--font-size); font-weight: var(--font-weight);\">which are special cases for which the created Event object is explicity assigned as properties. </span><span style=\"color: var(--text-color); font-family: var(--font-family); font-size: var(--font-size); font-weight: var(--font-weight);\">No attempt is made to verify whether those properties would reasonably exist on an Event of the created type.</span></div><div><span style=\"color: var(--text-color); font-family: var(--font-family); font-size: var(--font-size); font-weight: var(--font-weight);\"><br></span></div><div><span style=\"color: var(--text-color); font-family: var(--font-family); font-size: var(--font-size); font-weight: var(--font-weight);\">In addition, `init` may also define a `target` property and a `detail` property.\n</span></div><div><span style=\"color: var(--text-color); font-family: var(--font-family); font-size: var(--font-size); font-weight: var(--font-weight);\"><br></span></div><div><span style=\"color: var(--text-color); font-family: var(--font-family); font-size: var(--font-size); font-weight: var(--font-weight);\">`target`: should most probably take the shape of the same `node` that this Event is created against, </span><span style=\"color: var(--text-color); font-family: var(--font-family); font-size: var(--font-size); font-weight: var(--font-weight);\">thus, eg. a click event's `target` prop should point to the element that was clicked on, </span><span style=\"color: var(--text-color); font-family: var(--font-family); font-size: var(--font-size); font-weight: var(--font-weight);\">which should be the element passed as the first param to this function. However, </span><span style=\"color: var(--text-color); font-family: var(--font-family); font-size: var(--font-size); font-weight: var(--font-weight);\">its usage implies the existence of a couple of optional properties - `files` and `value`, </span><span style=\"color: var(--text-color); font-family: var(--font-family); font-size: var(--font-size); font-weight: var(--font-weight);\">whose types (taken from HTMLInputElement, their most likely containing object) I've explicitly intersected with their sole accesses below.\n</span></div><div><span style=\"color: var(--text-color); font-family: var(--font-family); font-size: var(--font-size); font-weight: var(--font-weight);\"><br></span></div><div>\r</div><div>`detail`: is only correctly used in the first case from the above-mentioned environmental Event class considerations, where it is passed to the event constructor.\r <span style=\"color: var(--text-color); font-family: var(--font-family); font-size: var(--font-size); font-weight: var(--font-weight);\">In those cases its type is varied, and dependent on the Event in question.</span></div>"
115+
},
116+
"id": "603e6503-4d39-42df-a812-7786f5081c1f",
117+
"type": "markdown"
118+
},
119+
{
120+
"blockId": "170a3b9f-2e74-4693-8962-9468e85c7b25",
121+
"data": {
122+
"contents": " *",
123+
"file": "src/events.ts",
124+
"range": {
125+
"end": {
126+
"character": 2147483647,
127+
"line": 53
128+
},
129+
"start": {
130+
"character": 0,
131+
"line": 53
132+
}
133+
},
134+
"scm": {
135+
"authors": [],
136+
"branch": "pr/ts/src/events",
137+
"file": "src/events.ts",
138+
"originalRange": {
139+
"end": {
140+
"character": 2147483647,
141+
"line": 53
142+
},
143+
"start": {
144+
"character": 0,
145+
"line": 53
146+
}
147+
},
148+
"remotes": [
149+
{
150+
"name": "origin",
151+
"url": "github.com/riotrah/dom-testing-library"
152+
},
153+
{
154+
"name": "upstream",
155+
"url": "github.com/testing-library/dom-testing-library"
156+
}
157+
],
158+
"repoName": "dom-testing-library",
159+
"revision": "458ef8738cb56de666899737d9b9aa94b32c9153",
160+
"sameRepo": true
161+
}
162+
},
163+
"id": "170a3b9f-2e74-4693-8962-9468e85c7b25",
164+
"type": "code",
165+
"version": 1
166+
}
167+
],
168+
"graphViewMetadata": {
169+
"edges": [
170+
{
171+
"id": "10ecf944-1813-4dad-813a-4e3e3a9ce05e_bottom_22da9304-9908-4580-a3df-c1633c7be0c1_top_MTjQTiyqSlaHdlCxYvI-fQ",
172+
"source": "10ecf944-1813-4dad-813a-4e3e3a9ce05e",
173+
"sourceHandle": "bottom",
174+
"target": "22da9304-9908-4580-a3df-c1633c7be0c1",
175+
"targetHandle": "top"
176+
},
177+
{
178+
"id": "3dd1f069-6d23-4bac-856d-2da380292acd_top_10ecf944-1813-4dad-813a-4e3e3a9ce05e_bottom_07DL6JvnROmfE-V0WNsDBQ",
179+
"source": "3dd1f069-6d23-4bac-856d-2da380292acd",
180+
"sourceHandle": "top",
181+
"target": "10ecf944-1813-4dad-813a-4e3e3a9ce05e",
182+
"targetHandle": "bottom"
183+
},
184+
{
185+
"id": "603e6503-4d39-42df-a812-7786f5081c1f_left_22da9304-9908-4580-a3df-c1633c7be0c1_right_ZEEz4CigQxmMbyYnB2J4RA",
186+
"source": "603e6503-4d39-42df-a812-7786f5081c1f",
187+
"sourceHandle": "left",
188+
"target": "22da9304-9908-4580-a3df-c1633c7be0c1",
189+
"targetHandle": "right"
190+
},
191+
{
192+
"id": "170a3b9f-2e74-4693-8962-9468e85c7b25_bottom_10ecf944-1813-4dad-813a-4e3e3a9ce05e_left_zuPisN4TQTmpQGeF-FVzLA",
193+
"source": "170a3b9f-2e74-4693-8962-9468e85c7b25",
194+
"sourceHandle": "bottom",
195+
"target": "10ecf944-1813-4dad-813a-4e3e3a9ce05e",
196+
"targetHandle": "left"
197+
}
198+
],
199+
"graph": {
200+
"position": [257.09839600544933, 100.8498721136998],
201+
"zoom": 1.0852293718818073
202+
},
203+
"nodes": [
204+
{
205+
"blockId": "10ecf944-1813-4dad-813a-4e3e3a9ce05e",
206+
"dimensions": {
207+
"height": 224,
208+
"width": 577
209+
},
210+
"id": "dndnode_0",
211+
"position": {
212+
"x": 105,
213+
"y": 45
214+
}
215+
},
216+
{
217+
"blockId": "22da9304-9908-4580-a3df-c1633c7be0c1",
218+
"dimensions": {},
219+
"id": "dndnode_1",
220+
"position": {
221+
"x": 270,
222+
"y": 375
223+
}
224+
},
225+
{
226+
"blockId": "3dd1f069-6d23-4bac-856d-2da380292acd",
227+
"dimensions": {},
228+
"id": "dndnode_2",
229+
"position": {
230+
"x": -285,
231+
"y": 375
232+
}
233+
},
234+
{
235+
"blockId": "603e6503-4d39-42df-a812-7786f5081c1f",
236+
"dimensions": {
237+
"height": 866,
238+
"width": 537
239+
},
240+
"id": "dndnode_3",
241+
"position": {
242+
"x": 930,
243+
"y": 45
244+
}
245+
},
246+
{
247+
"blockId": "170a3b9f-2e74-4693-8962-9468e85c7b25",
248+
"dimensions": {},
249+
"id": "dndnode_4",
250+
"position": {
251+
"x": -270,
252+
"y": -90
253+
}
254+
}
255+
]
256+
},
257+
"text": "CreateEvent has a complex function signature if we try too hard to narrow the type of its `init` parameter or try to consider whether or not the environment its called in is IE11 (or otherwise doesn't define an Event-like class with the key `typeof eventType` in Window).\n\nIn general, I err on the side of assuming it's not invoked in IE11.",
258+
"title": "The type of CreateEvent (as called as a function)",
259+
"version": 1
260+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"cells": [],
3+
"graphViewMetadata": {
4+
"edges": [],
5+
"nodes": []
6+
},
7+
"text": "For the definitions of both _createEvent and _fireEvent, converting from `function _creatEvent() {}` to `const _createEvent: CreateFunction = () => {}` (etc) allows us to explicitly check that the exported types (which are imported here and assigned to the functions) match their written types/signatures. This also allows us to eventually export them as themselves, but `Required`. Eg: `export const createEvent = _createEvent as Required<typeof _createEvent>`, instead of a stronger, potentially more likely to be incorrect, cast of `_createEvent as CreateObject & CreateFunction`.\n\nThe downsides are:\n\n1. Loss of the explicit, quick designation as function that `function myFunc() {}` provides vs `const myFunc = () => {}`\n2. Unpredictable or different expectations for `this` when fireEvent or createEvent are invoked as methods, for example, as a result of becoming arrow functions\n3. A little uglier and slightly more mental overhead when reading/modifying this code in the future",
8+
"title": "Function definition syntax signature to arrow syntax",
9+
"version": 1
10+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"cells": [],
3+
"graphViewMetadata": {
4+
"edges": [],
5+
"graph": {
6+
"position": [1103.0797527512682, -472.6089894708548],
7+
"zoom": 0.9421312739048091
8+
},
9+
"nodes": []
10+
},
11+
"text": "In the existing `events.d.ts` `EventType` is a manually defied union of string literals. Im this portentially redundant definitions it's the keys of the the `eventMap` objet, which I suspect serves as a better source of truth for `EventType`. Stlll, so as to not break anything that depended on the existing definition as distinct from its usage in parameters of `fireEvent`/`createEvent` (however un/common that might be), I've kept their defs distinct here.",
12+
"title": "EventType in this file is partially redundant yet distinct from another similar type",
13+
"version": 1
14+
}

0 commit comments

Comments
 (0)