Skip to content
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

Type resolve-value #41

Merged
merged 2 commits into from
Mar 15, 2022
Merged

Type resolve-value #41

merged 2 commits into from
Mar 15, 2022

Conversation

rubenswieringa
Copy link
Contributor

@rubenswieringa rubenswieringa commented Feb 23, 2022

Changes in this PR allow you to use methods on the returned provider (in a TS project) without having to homecook your own type in your own codebase:

before:

type Provider = { on: (eventType: string, handler: () => unknown) => void }; // homecooking my own type
const provider = (await detectEthereumProvider()) as Provider;
provider.on('whenever', whatever);

now:

const provider = await detectEthereumProvider();
provider!.on('whenever', whatever);

or:

const provider = await detectEthereumProvider<MyCustomProvider>();
provider!.customProviderMethod();

Would love some feedback on whether this is a good idea or on why perhaps this might not be a great idea.

@rubenswieringa rubenswieringa requested a review from a team as a code owner February 23, 2022 12:20
rekmarks
rekmarks previously approved these changes Mar 10, 2022
Copy link
Member

@rekmarks rekmarks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks strictly better, thanks!

@rekmarks
Copy link
Member

Ah, @rubenswieringa, build error:

Error: src/index.ts(70,17): error TS2345: Argument of type 'EthereumProvider' is not assignable to parameter of type 'T | PromiseLike<T | null> | null | undefined'.

Easier if you want to do:

const provider = await detectEthereumProvider();
provider.on('whenever', whatever);
If used in a codebase where an ethereum-provider is expected that doesn’t have an on() method and instead has a different API.
Copy link
Member

@rekmarks rekmarks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thank you, @rubensw!

@rekmarks rekmarks merged commit c34bd8e into MetaMask:main Mar 15, 2022
This was referenced Oct 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants