-
Notifications
You must be signed in to change notification settings - Fork 5k
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
Fix TS types for subscription categories #3162
Conversation
@Velenir Ah yes, I see what you're saying. (Thanks so much for looking this over - I'm not a typescript expert.) The types at @types/web3 mirror the more permissive grammar. subscribe(
type: "syncing",
callback?: Callback<any>
): Promise<Subscribe<any>>;
subscribe(
type: "newBlockHeaders",
callback?: Callback<BlockHeader>
): Promise<Subscribe<BlockHeader>>;
subscribe(
type: "pendingTransactions",
callback?: Callback<Transaction>
): Promise<Subscribe<Transaction>>;
subscribe(
type: "pendingTransactions" | "newBlockHeaders" | "syncing" | "logs",
options?: Logs,
callback?: Callback<any>
): Promise<Subscribe<any>>; Can you suggest why they might have written the final overload that way? Am slightly wary of changing the rule without understanding why the existing pattern is in place. Is it just a mistake? I'm pinging @joshstevens19 for an additional peek at this as well... |
Great that you brought up that point. My guess would be that final overload was there to match all possible cases of event type, like: // we don't know which one it will be
const event: "pendingTransactions" | "newBlockHeaders" | "syncing" | "logs" = ...
// before, when all overloads had `options?`,
// the subscribe signature was virtually the same so the following was possible
// still don't know what event is, but don't care, it will work anyway
web3.eth.subscribe(event, options, console.log)
///practical application:
const events: ("pendingTransactions" | "newBlockHeaders" | "syncing" | "logs")[] =
['pendingTransactions', 'syncing', 'newBlockHeaders', 'logs']
events.forEach((ev) => eth.subscribe(ev, console.log)} // breaks for 'logs'
// have to actually check event
events.forEach((ev) => ev === 'logs' ? eth.subscribe(ev, {}, console.log) : eth.subscribe(ev, console.log)} But now we know that So we need an overload for all events except for TS is hard, but that was interesting |
Hey so the last overload should not be there. It is a very pointless type and will confuse TS completely let's kill it. From looking at it the types are all correct we should just remove that last overload and it will work fine and fix that issue for people on TS. subscribe(
type: 'logs',
options?: LogsOptions,
callback?: (error: Error, log: Log) => void
): Subscription<Log>;
subscribe(
type: 'syncing',
callback?: (error: Error, result: Syncing) => void
): Subscription<Syncing>;
subscribe(
type: 'newBlockHeaders',
callback?: (error: Error, blockHeader: BlockHeader) => void
): Subscription<BlockHeader>;
subscribe(
type: 'pendingTransactions',
callback?: (error: Error, transactionHash: string) => void
): Subscription<string>; one thing i did see if i'm not sure
👍 |
@@ -83,17 +83,14 @@ export class Eth { | |||
): Subscription<Log>; |
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.
just murder the last overload on like 96 and should be all good to merge.
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.
Thanks @joshstevens19!
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.
LGTM - we need to do this in the 2.0 branch as well if there’s any point, as there probably will not be anymore 2.x releases until next web3 completely. 🧐
@Velenir Am following your original proposal & Josh's recs to remove the overloading altogether and make the LogsOptions non-optional. LGTM with respect to docs & code. Thanks again, super helpful. |
#3159
In web3-eth/src/index.js,
syncing
,newBlockHeaders
, andpendingTransactions
do not expect any options param.The 1.x documentation for these subscriptions is correct.