New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[node] remove lib: "dom"; add global Event and EventTarget, fix other global types #59905
Merged
typescript-bot
merged 16 commits into
DefinitelyTyped:master
from
thw0rted:node-blob-fix
Oct 3, 2022
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
d9fef11
[node] Fix Blob definition in consumers
thw0rted 715df24
[node] Remove "dom" lib ref, add DOM-like Event
thw0rted 783e9dd
Conditionally expose global DOM EventTarget
thw0rted 2770063
[node] Take out top level Event declaration
thw0rted 81b61c9
Reconcile Node Event with DOM version
thw0rted 00417c0
Missed one reference
thw0rted a49481d
Merge branch 'master' into node-blob-fix
thw0rted 9060bc0
Bring tests up to date with latest master branch
thw0rted 70a166f
Expose global constructor aliases
thw0rted 4ef8e7e
Expose Event/EventTarget globally
thw0rted 683256d
Clarify NodeEventTarget
thw0rted e5e6b02
Conditionally expose type side of Event(Target)
thw0rted a84fa7a
Move DOM Event to its own file
thw0rted b71250f
Merge branch 'master' into node-blob-fix
thw0rted 3ef3469
Merge branch 'master' into node-blob-fix
thw0rted 542cacd
Merge branch 'master' into node-blob-fix
thw0rted File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
export {}; // Don't export anything! | ||
|
||
//// DOM-like Events | ||
// NB: The Event / EventTarget / EventListener implementations below were copied | ||
// from lib.dom.d.ts, then edited to reflect Node's documentation at | ||
// https://nodejs.org/api/events.html#class-eventtarget. | ||
// Please read that link to understand important implementation differences. | ||
|
||
// This conditional type will be the existing global Event in a browser, or | ||
// the copy below in a Node environment. | ||
type __Event = typeof globalThis extends { onmessage: any, Event: infer T } | ||
? T | ||
: { | ||
/** This is not used in Node.js and is provided purely for completeness. */ | ||
readonly bubbles: boolean; | ||
/** Alias for event.stopPropagation(). This is not used in Node.js and is provided purely for completeness. */ | ||
cancelBubble: () => void; | ||
/** True if the event was created with the cancelable option */ | ||
readonly cancelable: boolean; | ||
/** This is not used in Node.js and is provided purely for completeness. */ | ||
readonly composed: boolean; | ||
/** Returns an array containing the current EventTarget as the only entry or empty if the event is not being dispatched. This is not used in Node.js and is provided purely for completeness. */ | ||
composedPath(): [EventTarget?] | ||
/** Alias for event.target. */ | ||
readonly currentTarget: EventTarget | null; | ||
/** Is true if cancelable is true and event.preventDefault() has been called. */ | ||
readonly defaultPrevented: boolean; | ||
/** This is not used in Node.js and is provided purely for completeness. */ | ||
readonly eventPhase: 0 | 2; | ||
/** The `AbortSignal` "abort" event is emitted with `isTrusted` set to `true`. The value is `false` in all other cases. */ | ||
readonly isTrusted: boolean; | ||
/** Sets the `defaultPrevented` property to `true` if `cancelable` is `true`. */ | ||
preventDefault(): void; | ||
/** This is not used in Node.js and is provided purely for completeness. */ | ||
returnValue: boolean; | ||
/** Alias for event.target. */ | ||
readonly srcElement: EventTarget | null; | ||
/** Stops the invocation of event listeners after the current one completes. */ | ||
stopImmediatePropagation(): void; | ||
/** This is not used in Node.js and is provided purely for completeness. */ | ||
stopPropagation(): void; | ||
/** The `EventTarget` dispatching the event */ | ||
readonly target: EventTarget | null; | ||
/** The millisecond timestamp when the Event object was created. */ | ||
readonly timeStamp: number; | ||
/** Returns the type of event, e.g. "click", "hashchange", or "submit". */ | ||
readonly type: string; | ||
}; | ||
|
||
// See comment above explaining conditional type | ||
type __EventTarget = typeof globalThis extends { onmessage: any, EventTarget: infer T } | ||
? T | ||
: { | ||
/** | ||
* Adds a new handler for the `type` event. Any given `listener` is added only once per `type` and per `capture` option value. | ||
* | ||
* If the `once` option is true, the `listener` is removed after the next time a `type` event is dispatched. | ||
* | ||
* The `capture` option is not used by Node.js in any functional way other than tracking registered event listeners per the `EventTarget` specification. | ||
* Specifically, the `capture` option is used as part of the key when registering a `listener`. | ||
* Any individual `listener` may be added once with `capture = false`, and once with `capture = true`. | ||
*/ | ||
addEventListener( | ||
type: string, | ||
listener: EventListener | EventListenerObject, | ||
options?: AddEventListenerOptions | boolean, | ||
): void; | ||
/** Dispatches a synthetic event event to target and returns true if either event's cancelable attribute value is false or its preventDefault() method was not invoked, and false otherwise. */ | ||
dispatchEvent(event: Event): boolean; | ||
/** Removes the event listener in target's event listener list with the same type, callback, and options. */ | ||
removeEventListener( | ||
type: string, | ||
listener: EventListener | EventListenerObject, | ||
options?: EventListenerOptions | boolean, | ||
): void; | ||
}; | ||
|
||
interface EventInit { | ||
bubbles?: boolean; | ||
cancelable?: boolean; | ||
composed?: boolean; | ||
} | ||
|
||
interface EventListenerOptions { | ||
/** Not directly used by Node.js. Added for API completeness. Default: `false`. */ | ||
capture?: boolean; | ||
} | ||
|
||
interface AddEventListenerOptions extends EventListenerOptions { | ||
/** When `true`, the listener is automatically removed when it is first invoked. Default: `false`. */ | ||
once?: boolean; | ||
/** When `true`, serves as a hint that the listener will not call the `Event` object's `preventDefault()` method. Default: false. */ | ||
passive?: boolean; | ||
} | ||
|
||
interface EventListener { | ||
(evt: Event): void; | ||
} | ||
|
||
interface EventListenerObject { | ||
handleEvent(object: Event): void; | ||
} | ||
|
||
import {} from 'events'; // Make this an ambient declaration | ||
declare global { | ||
/** An event which takes place in the DOM. */ | ||
interface Event extends __Event {} | ||
var Event: typeof globalThis extends { onmessage: any, Event: infer T } | ||
? T | ||
: { | ||
prototype: __Event; | ||
new (type: string, eventInitDict?: EventInit): __Event; | ||
}; | ||
|
||
/** | ||
* EventTarget is a DOM interface implemented by objects that can | ||
* receive events and may have listeners for them. | ||
*/ | ||
interface EventTarget extends __EventTarget {} | ||
var EventTarget: typeof globalThis extends { onmessage: any, EventTarget: infer T } | ||
? T | ||
: { | ||
prototype: __EventTarget; | ||
new (): __EventTarget; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm surprised this passes tests given that
dom-events
should be parsed as a module.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, well, I may be showing a lot of ignorance in this PR but at least I'm learning a lot too.
Is the opposite of "parsed as a module" supposed to be "parsed as a script"? And all the other
reference
d files parse as a script because at the top level they onlydeclare module 'xyz'
? (Or in the case ofglobals.d.ts
, declare types that will be added to the global scope.)Should I refactor the DOM event declarations to parse as a script? (Is that possible without exporting all the intermediate types?) If not, would it be more idiomatically correct to use a side-effect
import
statement instead? I was just aping the syntax used to incorporate the other files, not realizing how they were different.