Skip to content

feat: Add PublicInterface type #197

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

Merged
merged 1 commit into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/misc.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
import { expectAssignable, expectNotAssignable, expectType } from 'tsd';

import type { RuntimeObject } from './misc';
import type { PublicInterface, RuntimeObject } from './misc';
import { isObject, hasProperty, getKnownPropertyNames } from './misc';

//=============================================================================
// PublicInterface
//=============================================================================

class ClassWithPrivateProperties {
#foo: string;

bar: string;

constructor({ foo, bar }: { foo: string; bar: string }) {
this.#foo = foo;
this.bar = bar;
}
}

// Private properties not required
expectAssignable<PublicInterface<ClassWithPrivateProperties>>({ bar: 'bar' });
// Public properties still required
expectNotAssignable<PublicInterface<ClassWithPrivateProperties>>({});

//=============================================================================
// isObject
//=============================================================================
Expand Down
9 changes: 9 additions & 0 deletions src/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ export type Mutable<
[Key in keyof Omit<ObjectValue, TargetKey>]: ObjectValue[Key];
};

/**
* Get a type representing the public interface of the given type. The
* returned type will have all public properties, but will omit private
* properties.
*
* @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.


/**
* Useful for representing some value that _might_ be present and / or complete.
*
Expand Down
Loading