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

feat: Add PublicInterface type #197

Merged
merged 1 commit into from
Jul 12, 2024
Merged

feat: Add PublicInterface type #197

merged 1 commit into from
Jul 12, 2024

Conversation

Gudahtt
Copy link
Member

@Gudahtt Gudahtt commented Jul 11, 2024

Add a type for deriving the "public interface" of a type, excluding private properties. Excluding private properties can be useful in making our classes more easily testable (types with private properties are more difficult to mock).

@Gudahtt Gudahtt requested a review from a team as a code owner July 11, 2024 11:36
@Gudahtt
Copy link
Member Author

Gudahtt commented Jul 11, 2024

I had need of this type in this PR: https://github.com/MetaMask/metamask-extension/pull/24503/files#diff-ea83e113f95081f8e48d8927c7b9ed1c532a69cfcaa812753ffe101e2fc85e09R18

I was considering referencing it in this ADR: https://github.com/MetaMask/decisions/pull/31
Or at least using it in the example. Exporting a public interface type from service classes would make them easier to mock elsewhere in the codebase, which we'll commonly want to do.

Add a type for deriving the "public interface" of a type, excluding
private properties. Excluding private properties can be useful in
making our classes more easily testable (types with private properties
are more difficult to mock).
@Gudahtt Gudahtt requested review from a team July 11, 2024 11:56
Copy link
Contributor

@mcmire mcmire left a comment

Choose a reason for hiding this comment

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

This is cool! I wasn't aware that this was possible. LGTM.

*
* @template Interface - The interface to return a public representation of.
*/
export type PublicInterface<Interface> = Pick<Interface, keyof Interface>;
Copy link
Member Author

Choose a reason for hiding this comment

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

My only concern with this is the name. It's a little weird to call this "PublicInterface" when it's a type, not an interface.

I meant "interface" in the English word sense, not like a TypeScript interface, but maybe there's a better name that can avoid this confusion.

I couldn't think of one though, and this seems good enough

Copy link
Member Author

Choose a reason for hiding this comment

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

I guess we could call it PublicType 🤔 That doesn't have the same connotation as "public interface" though. Probably best as-is.

@Gudahtt Gudahtt merged commit b8f869d into main Jul 12, 2024
19 checks passed
@Gudahtt Gudahtt deleted the add-public-interface-type branch July 12, 2024 17:47
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.

2 participants