change type assignments to interfaces for augmentation purposes#58753
Conversation
|
@cwadrupldijjit Thank you for submitting this PR! I see this is your first time submitting to DefinitelyTyped 👋 — I'm the local bot who will help you through the process of getting things through. This is a live comment which I will keep updated. 1 package in this PR
@cwadrupldijjit: I see that you have added yourself as an owner, are you sure you want to become an owner? Code ReviewsBecause this is a widely-used package, a DT maintainer will need to review it before it can be merged. You can test the changes of this PR in the Playground. Status
All of the items on the list are green. To merge, you need to post a comment including the string "Ready to merge" to bring in your changes. Diagnostic Information: What the bot saw about this PR{
"type": "info",
"now": "-",
"pr_number": 58753,
"author": "cwadrupldijjit",
"headCommitOid": "954895b186ea07c982148461f3ffbed0e729f5aa",
"mergeBaseOid": "7dec77e4522d220346c90fcd09044e78feb49dd0",
"lastPushDate": "2022-02-18T17:49:04.000Z",
"lastActivityDate": "2022-02-19T02:53:47.000Z",
"mergeOfferDate": "2022-02-18T23:37:44.000Z",
"mergeRequestDate": "2022-02-19T02:53:47.000Z",
"mergeRequestUser": "cwadrupldijjit",
"hasMergeConflict": false,
"isFirstContribution": true,
"tooManyFiles": false,
"hugeChange": false,
"popularityLevel": "Critical",
"pkgInfo": [
{
"name": "ws",
"kind": "edit",
"files": [
{
"path": "types/ws/index.d.ts",
"kind": "definition"
},
{
"path": "types/ws/ws-tests.ts",
"kind": "test"
}
],
"owners": [
"loyd",
"mlamp",
"TitaneBoy",
"reduckted",
"teidesu",
"wojtkowiak",
"k-yle"
],
"addedOwners": [
"cwadrupldijjit"
],
"deletedOwners": [],
"popularityLevel": "Critical"
}
],
"reviews": [
{
"type": "approved",
"reviewer": "andrewbranch",
"date": "2022-02-18T23:37:04.000Z",
"isMaintainer": true
},
{
"type": "approved",
"reviewer": "k-yle",
"date": "2022-02-18T20:35:11.000Z",
"isMaintainer": false
},
{
"type": "approved",
"reviewer": "vansergen",
"date": "2022-02-18T19:35:12.000Z",
"isMaintainer": false
},
{
"type": "stale",
"reviewer": "doroodgar",
"date": "2022-02-17T12:27:57.000Z",
"abbrOid": "d2c6c9f"
}
],
"mainBotCommentID": 1037950088,
"ciResult": "pass"
} |
|
🔔 @loyd @mlamp @TitaneBoy @reduckted @teidesu @wojtkowiak @k-yle — please review this PR in the next few days. Be sure to explicitly select |
|
Note: The second check through Azure DevOps seems to have failed due to TypeScript not being where it was expected, which doesn't have anything to do with my changes. |
|
Also another note: The |
k-yle
left a comment
There was a problem hiding this comment.
Thanks. Do we still need this if #58772 is merged? Also note that #58555 is yet another approach to solving this issue.
cc @mglyzin and @lavalleeale
|
I'd think both approaches could work simultaneously. In my case, I don't want to create a custom WebSocket class to use instead. I just would augment the original WebSocket slightly. To be fair, I could create a new class, but if it's only for adding a single property, that feels like a little much. But I could also swallow my pride and do it that way instead if that's just how it should be. Interested to hear others weigh in on this to see if it's necessary. |
|
Though thinking back to the other two PRs, the one that looks like it would do closer to what I want would be #58772. The other seems to make sense, too. Those coincide fine. But I might also note that my approach doesn't change the original class (such as if you import default or Example: // these can't be augmented
import WebSocket = require('ws');
import WebSocket from 'ws';
// these, however, can
import { WebSocket } from 'ws';
import * as wslib from 'ws';
declare const websocket: wslib.WebSocket;In my case, since I'm never creating a |
The discussion in websockets/ws#2007 suggests that both approaches are valid
I think you're right, we should go ahead with this PR and #58772, which would make #58555 redundant (unless I'm misunderstanding the benefit of that PR?) |
|
I'd say that the The result of mine is more related to adding to the original structure of the WebSocket objects instead of creating a new type to handle it. The I'm also personally for more flexibility than anything. I don't think any of those approaches are "wrong", though I think that the use cases for each approach might vary. Prefer to use your own WebSocket class? That's available for you. Want to fix the type of the Does that sound reasonable to you @k-yle and @lavalleeale? |
|
Since I anticipate there will be merge conflicts between the three for the tests, I'm willing to merge both of your branches into mine, fix the merge conflicts, and update this PR with those changes, if that makes sense and works for you. Or I'm willing to be the last one to deal with those in my code if you'd prefer. |
|
I think that sounds all good to me as long as the tests reflect all of the use cases |
|
Do we really need tests here?🤔 Otherwise LGTM |
|
|
||
| declare module 'ws' { | ||
| interface WebSocket { | ||
| id?: string; | ||
| } | ||
| } | ||
|
|
||
| { | ||
| const ws: wslib.WebSocket = new wslib.WebSocket('ws://www.host.com/path'); | ||
|
|
||
| // $ExpectType string | undefined | ||
| ws.id; | ||
| ws.id = 'foo'; | ||
| } |
There was a problem hiding this comment.
| declare module 'ws' { | |
| interface WebSocket { | |
| id?: string; | |
| } | |
| } | |
| { | |
| const ws: wslib.WebSocket = new wslib.WebSocket('ws://www.host.com/path'); | |
| // $ExpectType string | undefined | |
| ws.id; | |
| ws.id = 'foo'; | |
| } |
Module augmentation is the feature in TypeScript. I do not think that there is a need to test explicitly, like
declare module 'ws' {
interface WebSocket {There was a problem hiding this comment.
True, it's a feature in TypeScript, but if it's ever changed back to a type assignment, then that test would fail. If the test isn't necessary, fine with me.
Perhaps I should put together another test next to it showing that the type would prevent augmentation?
There was a problem hiding this comment.
but if it's ever changed back to a type assignment, then that test would fail.
Agree, but the owners of the package would not allow that (I think) because of what we discuss here. I would just leave the test here to explain the motivation of this PR (as an example of usage).
There was a problem hiding this comment.
I this test is worth keeping in case someone tries to change this back to a type assignment. A comment linking to this PR would be helpful but not required.
@cwadrupldijjit, do you want to merge this PR first, as it is now? I'm happy to approve this now and deal with conflicts in the other two PRs
There was a problem hiding this comment.
I'm fine with that. I don't mind which order things are merged together. I'm also fine with helping to deal with the merge conflicts.
There was a problem hiding this comment.
this test is worth keeping in case someone tries to change this back to a type assignment.
Let's keep it if this is the concern. Is something else blocking this PR from merging?
There was a problem hiding this comment.
Since we want to keep the test to prevent changing interface to type, we should add a test for the Server class as well (for consistency).
@cwadrupldijjit Could you consider adding it? Something like
declare module 'ws' {
interface Server {
id?: string;
}
}
{
const ws = new wslib.Server({});
// $ExpectType string | undefined
ws.id;
ws.id = 'foo';
}should be enough.
There was a problem hiding this comment.
@vansergen, that's a great point. Let me add that real quick.
There was a problem hiding this comment.
I've adjusted the test to include a server augmentation, too. Could you, @vansergen and @k-yle, review it and see if it looks good to you?
|
@cwadrupldijjit The CI build failed! Please review the logs for more information. Once you've pushed the fixes, the build will automatically re-run. Thanks! Note: builds which are failing do not end up on the list of PRs for the DT maintainers to review. |
|
Alas... A blank line that broke the CI... |
|
@vansergen, @k-yle, @doroodgar Thank you for reviewing this PR! The author has pushed new commits since your last review. Could you take another look and submit a fresh review? |
|
My heart about stopped when I thought that all of the checks failed again, even though I watched the GitHub action run to successful completion... Sigh of relief when I refreshed the page and saw that it said all checks had passed. |
|
@k-yle, @doroodgar Thank you for reviewing this PR! The author has pushed new commits since your last review. Could you take another look and submit a fresh review? |
|
@doroodgar Thank you for reviewing this PR! The author has pushed new commits since your last review. Could you take another look and submit a fresh review? |
|
@cwadrupldijjit: Everything looks good here. I am ready to merge this PR (at 954895b) on your behalf whenever you think it's ready. If you'd like that to happen, please post a comment saying:
and I'll merge this PR almost instantly. Thanks for helping out! ❤️ (@loyd, @mlamp, @TitaneBoy, @reduckted, @teidesu, @wojtkowiak, @k-yle: you can do this too.) |
|
Ready to merge |
…s for augmentation purposes by @cwadrupldijjit * change type assignments to interfaces for augmentation purposes * fix tests for expected type for augmentation and no-empty-interfaces * add server augmentation test * Remove the failing blank line
Motivations for this change
I sought to augment the WebSocket implementation for a project I'm working on and I discovered that because of the
typeassignments (such as thetype WebSocket = WebSocketAlias;) made such a thing impossible. I imagine some implementations with WS could benefit from type augmentation, similar to how Express handles theRequestandResponseobjects.PR Checklist
npm test <package to test>.If changing an existing definition: