Skip to content

Commit 487a8ba

Browse files
authored
🤖 Merge PR DefinitelyTyped#71839 [AFRAME] improve types for query selectors and improve DOM type compatibility by @nyan-left
1 parent 3002c74 commit 487a8ba

File tree

3 files changed

+27
-19
lines changed

3 files changed

+27
-19
lines changed

‎types/aframe/index.d.ts‎

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ export interface ANode extends HTMLElement {
3232
// attachedCallback
3333
// attributeChangedCallback
3434
closestScene(): Scene;
35-
closest(selector: string): ANode;
35+
closest(selector: `a-${string}`): Entity | null;
36+
closest(selector: string): Element | null;
3637
// detachedCallback
3738
hasLoaded: boolean;
3839
load(cb?: () => void, childFilter?: (el: Element) => boolean): void;
@@ -111,7 +112,7 @@ export interface DefaultComponents {
111112
scale: Component<Coordinate>;
112113
}
113114

114-
export interface Entity<C = ObjectMap<Component>> extends ANode {
115+
export interface Entity<C = ObjectMap<any>> extends ANode {
115116
components: C & DefaultComponents;
116117
hasLoaded: boolean;
117118
isPlaying: boolean;
@@ -475,19 +476,11 @@ declare global {
475476
/**
476477
* Custom elements augment document methods to return custom HTML
477478
*/
478-
interface Document {
479-
createElement(tagName: string): Entity;
480-
querySelector(selectors: "a-scene"): Scene;
481-
querySelector(selectors: string): Entity<any>;
482-
querySelectorAll(selectors: string): NodeListOf<Entity<any> | Element>;
479+
interface AFrameElements {
480+
"a-scene": Scene;
481+
[key: `a-${string}`]: Entity;
483482
}
484483

485-
interface HTMLCollection extends HTMLCollectionBase {
486-
/**
487-
* Retrieves a select object or an object from an options collection.
488-
*/
489-
namedItem(name: string): Element | Entity | null;
490-
item(index: number): Element | Entity;
491-
[index: number]: Element | Entity;
492-
}
484+
// eslint-disable-next-line @typescript-eslint/no-empty-interface
485+
interface HTMLElementTagNameMap extends AFrameElements {}
493486
}

‎types/aframe/test/aframe-io-tests.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,8 @@ function getRandomColor() {
263263
}
264264

265265
// set sky values
266-
sky.setAttribute("color", `#${getRandomColor()}`);
267-
sky.setAttribute(
266+
sky?.setAttribute("color", `#${getRandomColor()}`);
267+
sky?.setAttribute(
268268
"animation__color",
269269
`property: color; dir: alternate; dur: 2000; easing: easeInOutSine; loop: true; to: #${getRandomColor()}`,
270270
);
@@ -329,7 +329,7 @@ function generateAllElements() {
329329
);
330330

331331
// append element to main container
332-
objectContainer.appendChild(rotateContainer);
332+
objectContainer?.appendChild(rotateContainer);
333333
}
334334
}
335335
}

‎types/aframe/test/aframe-tests.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ const Component = registerComponent("test-component", {
9797

9898
// Scene
9999
const scene = document.querySelector("a-scene");
100-
scene.hasLoaded;
100+
scene?.hasLoaded;
101101

102102
// System
103103

@@ -126,3 +126,18 @@ AFRAME.registerGeometry("a-test-geometry", {
126126
temp;
127127
},
128128
});
129+
130+
// DOM types unaffected
131+
132+
const div = document.createElement("div");
133+
134+
const height: number = div.clientHeight;
135+
136+
const button = document.querySelector<HTMLButtonElement>("button");
137+
button?.click();
138+
139+
const div2 = document.createElement("div");
140+
const height2: number = div2.clientHeight;
141+
142+
const button2 = document.querySelector("button");
143+
button2?.click();

0 commit comments

Comments
 (0)