Skip to content

Commit 42e7b45

Browse files
authored
refactor: Remove declare global from internal types (#4583)
* refactor: Remove `declare global` from internal types * refactor: Fix hook types
1 parent b9ff1b7 commit 42e7b45

File tree

14 files changed

+249
-226
lines changed

14 files changed

+249
-226
lines changed

hooks/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ export function useId() {
423423
const state = getHookState(currentIndex++, 11);
424424
if (!state._value) {
425425
// Grab either the root node or the nearest async boundary node.
426-
/** @type {import('./internal.d').VNode} */
426+
/** @type {import('./internal').VNode} */
427427
let root = currentComponent._vnode;
428428
while (root !== null && !root._mask && root._parent !== null) {
429429
root = root._parent;

hooks/src/internal.d.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
import {
2+
Options as PreactOptions,
3+
Component as PreactComponent,
4+
VNode as PreactVNode,
5+
Context as PreactContext,
6+
} from '../../src/internal';
17
import { Reducer, StateUpdater } from '.';
28

39
export { PreactContext };
410

5-
export interface Options extends globalThis.Options {
11+
export interface Options extends PreactOptions {
612
/** Attach a hook that is invoked before a vnode is diffed. */
713
_diff?(vnode: VNode): void;
814
diffed?(vnode: VNode): void;
@@ -24,14 +30,14 @@ export interface ComponentHooks {
2430
_pendingEffects: EffectHookState[];
2531
}
2632

27-
export interface Component extends globalThis.Component<any, any> {
33+
export interface Component extends PreactComponent<any, any> {
2834
__hooks?: ComponentHooks;
2935
// Extend to include HookStates
3036
_renderCallbacks?: Array<HookState | (() => void)>;
3137
_hasScuFromHooks?: boolean;
3238
}
3339

34-
export interface VNode extends globalThis.VNode {
40+
export interface VNode extends PreactVNode {
3541
_mask?: [number, number];
3642
_component?: Component; // Override with our specific Component type
3743
}

src/clone-element.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import { UNDEFINED } from './constants';
55
/**
66
* Clones the given VNode, optionally adding attributes/props and replacing its
77
* children.
8-
* @param {VNode} vnode The virtual DOM element to clone
8+
* @param {import('./internal').VNode} vnode The virtual DOM element to clone
99
* @param {object} props Attributes/props to add when cloning
10-
* @param {Array<ComponentChildren>} rest Any additional arguments will be used
10+
* @param {Array<import('./internal').ComponentChildren>} rest Any additional arguments will be used
1111
* as replacement children.
12-
* @returns {VNode}
12+
* @returns {import('./internal').VNode}
1313
*/
1414
export function cloneElement(vnode, props, children) {
1515
let normalizedProps = assign({}, vnode.props),

src/component.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export function BaseComponent(props, context) {
1818

1919
/**
2020
* Update component state and schedule a re-render.
21-
* @this {Component}
21+
* @this {import('./internal').Component}
2222
* @param {object | ((s: object, p: object) => object)} update A hash of state
2323
* properties to update with new values or a function that given the current
2424
* state and props returns a new partial state
@@ -57,7 +57,7 @@ BaseComponent.prototype.setState = function (update, callback) {
5757

5858
/**
5959
* Immediately perform a synchronous re-render of the component
60-
* @this {Component}
60+
* @this {import('./internal').Component}
6161
* @param {() => void} [callback] A function to be called after component is
6262
* re-rendered
6363
*/
@@ -85,7 +85,7 @@ BaseComponent.prototype.forceUpdate = function (callback) {
8585
BaseComponent.prototype.render = Fragment;
8686

8787
/**
88-
* @param {VNode} vnode
88+
* @param {import('./internal').VNode} vnode
8989
* @param {number | null} [childIndex]
9090
*/
9191
export function getDomSibling(vnode, childIndex) {
@@ -118,7 +118,7 @@ export function getDomSibling(vnode, childIndex) {
118118

119119
/**
120120
* Trigger in-place re-rendering of a component.
121-
* @param {Component} component The component to rerender
121+
* @param {import('./internal').Component} component The component to rerender
122122
*/
123123
function renderComponent(component) {
124124
let oldVNode = component._vnode,
@@ -155,7 +155,7 @@ function renderComponent(component) {
155155
}
156156

157157
/**
158-
* @param {VNode} vnode
158+
* @param {import('./internal').VNode} vnode
159159
*/
160160
function updateParentDomPointers(vnode) {
161161
if ((vnode = vnode._parent) != null && vnode._component != null) {
@@ -174,7 +174,7 @@ function updateParentDomPointers(vnode) {
174174

175175
/**
176176
* The render queue
177-
* @type {Array<Component>}
177+
* @type {Array<import('./internal').Component>}
178178
*/
179179
let rerenderQueue = [];
180180

@@ -196,7 +196,7 @@ const defer =
196196

197197
/**
198198
* Enqueue a rerender of a component
199-
* @param {Component} c The component to rerender
199+
* @param {import('./internal').Component} c The component to rerender
200200
*/
201201
export function enqueueRender(c) {
202202
if (
@@ -212,8 +212,8 @@ export function enqueueRender(c) {
212212
}
213213

214214
/**
215-
* @param {Component} a
216-
* @param {Component} b
215+
* @param {import('./internal').Component} a
216+
* @param {import('./internal').Component} b
217217
*/
218218
const depthSort = (a, b) => a._vnode._depth - b._vnode._depth;
219219

src/create-context.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ export function createContext(defaultValue, contextId) {
88
const context = {
99
_id: contextId,
1010
_defaultValue: defaultValue,
11-
/** @type {FunctionComponent} */
11+
/** @type {import('./internal').FunctionComponent} */
1212
Consumer(props, contextValue) {
1313
// return props.children(
1414
// context[contextId] ? context[contextId].props.value : defaultValue
1515
// );
1616
return props.children(contextValue);
1717
},
18-
/** @type {FunctionComponent} */
18+
/** @type {import('./internal').FunctionComponent} */
1919
Provider(props) {
2020
if (!this.getChildContext) {
21-
/** @type {Set<Component> | null} */
21+
/** @type {Set<import('./internal').Component> | null} */
2222
let subs = new Set();
2323
let ctx = {};
2424
ctx[contextId] = this;

src/create-element.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ let vnodeId = 0;
66

77
/**
88
* Create an virtual node (used for JSX)
9-
* @param {VNode["type"]} type The node name or Component constructor for this
9+
* @param {import('./internal').VNode["type"]} type The node name or Component constructor for this
1010
* virtual node
1111
* @param {object | null | undefined} [props] The properties of the virtual node
1212
* @param {Array<import('.').ComponentChildren>} [children] The children of the
1313
* virtual node
14-
* @returns {VNode}
14+
* @returns {import('./internal').VNode}
1515
*/
1616
export function createElement(type, props, children) {
1717
let normalizedProps = {},
@@ -44,20 +44,20 @@ export function createElement(type, props, children) {
4444

4545
/**
4646
* Create a VNode (used internally by Preact)
47-
* @param {VNode["type"]} type The node name or Component
47+
* @param {import('./internal').VNode["type"]} type The node name or Component
4848
* Constructor for this virtual node
4949
* @param {object | string | number | null} props The properties of this virtual node.
5050
* If this virtual node represents a text node, this is the text of the node (string or number).
5151
* @param {string | number | null} key The key for this virtual node, used when
5252
* diffing it against its children
53-
* @param {VNode["ref"]} ref The ref property that will
53+
* @param {import('./internal').VNode["ref"]} ref The ref property that will
5454
* receive a reference to its created child
55-
* @returns {VNode}
55+
* @returns {import('./internal').VNode}
5656
*/
5757
export function createVNode(type, props, key, ref, original) {
5858
// V8 seems to be better at detecting type shapes if the object is allocated from the same call site
5959
// Do not inline into createElement and coerceToVNode!
60-
/** @type {VNode} */
60+
/** @type {import('./internal').VNode} */
6161
const vnode = {
6262
type,
6363
props,

src/diff/catch-error.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
/**
22
* Find the closest error boundary to a thrown error and call it
33
* @param {object} error The thrown value
4-
* @param {VNode} vnode The vnode that threw the error that was caught (except
4+
* @param {import('../internal').VNode} vnode The vnode that threw the error that was caught (except
55
* for unmounting when this parameter is the highest parent that was being
66
* unmounted)
7-
* @param {VNode} [oldVNode]
8-
* @param {ErrorInfo} [errorInfo]
7+
* @param {import('../internal').VNode} [oldVNode]
8+
* @param {import('../internal').ErrorInfo} [errorInfo]
99
*/
1010
export function _catchError(error, vnode, oldVNode, errorInfo) {
11-
/** @type {Component} */
11+
/** @type {import('../internal').Component} */
1212
let component,
13-
/** @type {ComponentType} */
13+
/** @type {import('../internal').ComponentType} */
1414
ctor,
1515
/** @type {boolean} */
1616
handled;

src/diff/children.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ import {
1010
import { isArray } from '../util';
1111
import { getDomSibling } from '../component';
1212

13+
/**
14+
* @typedef {import('../internal').ComponentChildren} ComponentChildren
15+
* @typedef {import('../internal').Component} Component
16+
* @typedef {import('../internal').PreactElement} PreactElement
17+
* @typedef {import('../internal').VNode} VNode
18+
*/
19+
1320
/**
1421
* Diff the children of a virtual node
1522
* @param {PreactElement} parentDom The DOM element whose children are being

src/diff/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ import { setProperty } from './props';
1515
import { assign, isArray, removeNode, slice } from '../util';
1616
import options from '../options';
1717

18+
/**
19+
* @typedef {import('../internal').ComponentChildren} ComponentChildren
20+
* @typedef {import('../internal').Component} Component
21+
* @typedef {import('../internal').PreactElement} PreactElement
22+
* @typedef {import('../internal').VNode} VNode
23+
*/
24+
25+
/**
26+
* @template {any} T
27+
* @typedef {import('../internal').Ref<T>} Ref<T>
28+
*/
29+
1830
/**
1931
* Diff two virtual nodes and apply proper changes to the DOM
2032
* @param {PreactElement} parentDom The parent of the DOM element

src/diff/props.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ let eventClock = 0;
3030

3131
/**
3232
* Set a property value on a DOM node
33-
* @param {PreactElement} dom The DOM node to modify
33+
* @param {import('../internal').PreactElement} dom The DOM node to modify
3434
* @param {string} name The name of the property to set
3535
* @param {*} value The value to set the property to
3636
* @param {*} oldValue The old value the property had
@@ -152,7 +152,7 @@ export function setProperty(dom, name, value, oldValue, namespace) {
152152
function createEventProxy(useCapture) {
153153
/**
154154
* Proxy an event to hooked event handlers
155-
* @param {PreactEvent} e The event object from the browser
155+
* @param {import('../internal').PreactEvent} e The event object from the browser
156156
* @private
157157
*/
158158
return function (e) {

0 commit comments

Comments
 (0)