feat: transform Request, Response, and WebSocket classes to interfaces with var declarations #2708
+486
−0
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.
This PR is part of the drive for Node.js compatibility and focusses on ensuring 3 specific interfaces,
Request
,Response
, andWebSocket
, are compatible with@types/node
.The maintainers of
@types/node
considered a related use case when addingfetch
types to Node, because it's relatively common forlib.dom
and@types/node
to be loaded together, which would cause similar conflicts to what we're seeing now.Because of that, they use this pattern when defining a web type:
This defers to the global
Response
type if it's available, falling back toundici
otherwise.In order to get this to work for us, we need to declare
declare var onmessage: never
, thenResponse
will be ourResponse
.Next, we need to get these three interfaces onto
globalThis
. In order to do that, ourclass
delcarations need to be converted tovar
declarations. And in order to get the same type of behaviour (e.g. in return types), we need a corresponding interface. For example:Implementation
This PR creates two transformers that will transform our types in the worker:
createAddOnMessageDeclarationTransformer
- adds theonmessage
declaration.createClassToInterfaceTransformer
- converts our classes to an interface+var declaration pair.