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

Package Seal: magically make an interface sealed when it leaves the package! #21040

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

CraigMacomber
Copy link
Contributor

Description

TypeScript has different type checking within a compilation unit vs when using the d.ts files generated by that compilation. This can be used to modify a type when viewed from outside the package, allowing building the package to type check with one version, but for users to get a different version.

This is probably a bad idea, and very confusing, but it is possible.

It can be used to make a branded version of an interface that is not implementable, but leave it implementable when inside the package to avoid needing to cast to the non-implementable type when outputting instance via the public API.

In this case the branding is done by intersecting with ErasedType which is a non-implementable non constructable token type we already have laying around, but a different approach could be used if desired.
More advanced API transformations could be performed as well, like removing all members ending in "internal" or swapping the interface for some base interface with less members. This could be combined with the branding to make it relatively type safe.

These same approaches can be done across without the in package detection trick, and just for an explicit cast everywhere a value is exposed in the public API: that may be needed for repo instead of package scoped cases.

Reviewer Guidance

The review process is outlined on this wiki page.

If you think this might be useful, let me know.

@github-actions github-actions bot added area: dds Issues related to distributed data structures area: dds: tree area: examples Changes that focus on our examples area: framework Framework is a tag for issues involving the developer framework. Eg Aqueduct public api change Changes to a public API base: main PRs targeted against main branch labels May 10, 2024
Comment on lines +87 to +88
? T
: T & ErasedType<readonly ["PackageSeal", T]>;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here we get to pick how the type looks from inside the package and outside the package. This is branding it, but other things are possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: dds: tree area: dds Issues related to distributed data structures area: examples Changes that focus on our examples area: framework Framework is a tag for issues involving the developer framework. Eg Aqueduct base: main PRs targeted against main branch public api change Changes to a public API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant