FluidFramework icon indicating copy to clipboard operation
FluidFramework copied to clipboard

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

Open CraigMacomber opened this issue 9 months ago • 0 comments

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.

CraigMacomber avatar May 10 '24 00:05 CraigMacomber