🚀 Protect TypeScript Library: Ship Types, Hide Code Fast (2026)11 min read2,175 words

🚀 Protect TypeScript Library: Ship Types, Hide Code Fast (2026)

Protect your TypeScript library by shipping TypeScript declaration files, hide JavaScript implementation, and publish safely—learn what to exclude + test the tarball.

protect TypeScript libraryTypeScript package publishinghide JavaScript implementationTypeScript declaration filesJavaScript obfuscation
🚀 Protect TypeScript Library: Ship Types, Hide Code Fast (2026)

Publish a TypeScript Library Without Shipping Your Source Code

A TypeScript library usually has two "products" inside it: what the API consumers use, and the runtime code that actually implements that API. Your users want good types, autocomplete, and editor navigation. If you're selling the library, you may also want casual copying of your implementation to be less convenient.

You can do both - but only if you package intentionally. Type declarations tell callers what they can call. Compiled JavaScript is what runs. Client-side code never becomes truly secret just because it's minified or obfuscated: the consumer's machine still has to execute it. What those steps can do is reduce accidental disclosure and make copy/paste analysis a bit more annoying.

This guide walks through the package layout, how source maps and included files can defeat your plan, what protection options are realistic, and a release process that checks the actual published artifact. The goal isn't "invulnerable code." It's a clean public contract without shipping files you never meant to include.

How should you structure a TypeScript package for publication?

Treat declaration files and runtime files as separate deliverables. Declarations describe the public contract. Compiled JavaScript provides the implementation. In a perfect world, consumers can understand the contract without receiving your TypeScript source tree or internal build artifacts.

A package can expose declarations and distinct ESM/CommonJS entry points via something like this:

{

"name": "@acme/sdk",

"version": "2.4.0",

"main": "./dist/index.js",

"module": "./dist/index.mjs",

"types": "./dist/index.d.ts",

"exports": {

".": {

"types": "./dist/index.d.ts",

"import": "./dist/index.mjs",

"require": "./dist/index.js"

}

},

"files": [

"dist/index.js",

"dist/index.mjs",

"dist/index.d.ts",

"README.md",

"LICENSE"

],

"sideEffects": false

}

Here's the basic idea:

  • types and exports.types steer TypeScript toward the .d.ts files.
  • main/module/exports.import/exports.require point to compiled JavaScript, not .ts.
  • files is an allowlist. It's how you keep "oops, we forgot to exclude that directory" from turning into a published leak.

Before you reuse a manifest, check how it affects compatibility. The exports field controls which import paths consumers are allowed to use - if you stop exporting deep paths you used to allow, downstream code may break. The module field is used by many bundlers, but it's not a substitute for correct conditional exports when you care about consistent behavior across environments. Test the entry points your users actually import.

Make the declarations useful, not revealing

A good .d.ts file describes callable functions, exported types, and documented behavior. It shouldn't include function bodies. For example:

export interface QuoteInput {

seats: number;

plan: "free" | "pro";

coupon?: string;

}

export function quote(input: QuoteInput): Promise<Quote>;

export function verifyLicense(token: string): Promise<boolean>;

This gives users real autocomplete and compile-time checks without showing how quote computes its result. Also, "not revealing" is never absolute. Names, parameters, return types, and docs still tell people what your API looks like. That's the point of declarations. If you don't want to expose internal details, don't export private state, private heuristics, or implementation-specific constants as types unless consumers truly need them.

The same goes for comments. JSDoc is part of what you publish. Use it for valid inputs and intended behavior. Don't dump internal design notes or sensitive implementation details and then assume they'll stay hidden.

Which source maps and files can expose your implementation?

Even with clean entry points, a package can still leak implementation through maps or an overly broad npm archive. Source maps exist to help you go from generated code back to original code. That's great during development. In a public tarball, it can undo minification or obfuscation in practice.

JavaScript source maps often include a sourcesContent field with the original source text. Even if they don't, the map might reference source files that were included alongside the map. And generated files commonly include a //# sourceMappingURL=... comment that tells tools where the map is. If the map (or its referenced files) is inside the published package, a determined consumer can often inspect the source.

Declaration maps (declarationMap: true) create a similar problem. With declaration maps, editors can navigate from .d.ts back to the original .ts during "Go to Definition." If those original files are available after installation, you've made it easier to reach the implementation than you probably intended.

Configure a separate publish build

You can still use source maps in development, then disable them for the publish build. Having a separate configuration helps the team debug normally without accidentally shipping those artifacts. For example:

{

"compilerOptions": {

"declaration": true,

"declarationMap": false,

"sourceMap": false,

"inlineSources": false,

"inlineSourceMap": false

}

}

That configuration isn't a substitute for actually checking what you publish. Another build step might generate maps after TypeScript finishes, or a packaging rule might include something you didn't mean to ship. Inspect the output folder and the packed archive, not just the compiler settings.

A useful release check is to look for map files and map references:

find dist \( -name "*.map" -o -name "*.d.ts.map" \) -print

grep -R "sourceMappingURL" dist || true

If your published files are supposed to contain no maps, both checks should come back empty. Then review what's in the package: src/, tests, debug builds, and config files are common sources of accidental inclusion. A .d.ts file doesn't contain a function body, but an accidentally included src/engine.ts does.

What can you realistically do to protect published JavaScript?

First, be clear about what you're trying to prevent:

  • Accidental source inclusion → allowlists and tarball inspection work.
  • Discouraging casual copying → minification and ordinary obfuscation add friction.
  • Protecting a valuable commercial implementation → client-side protection can raise the cost of analysis, but it can't make the code secret.

Minification removes whitespace and often shortens variable names. It makes code harder to read, but it usually doesn't stop someone who's determined to understand behavior. Obfuscation can rename symbols, encode strings, or transform control flow. That also makes inspection and copy/paste less convenient, but it's still not a real security boundary - it adds complexity, and it can make debugging harder. It's an obstacle, not secrecy.

Protect only the valuable parts

Some commercial SDKs use VM-style protection on a small set of functions. Instead of shipping normal JavaScript control flow for that logic, the function is compiled into custom bytecode and run by an interpreter you bundle. This can slow reverse engineering, but it adds runtime overhead, compatibility concerns, and yet another build step to maintain.

Don't slap heavyweight protection on everything by default. It can slow your library down and make production issues harder to diagnose. Pick the few functions that contain the genuinely proprietary logic, test the protected output on the runtimes you support, and compare the performance cost to the value of protecting that logic. If a function isn't commercially sensitive, a normal compiled (or just minified) output is often the better trade-off.

Do not make a client-side license check your security boundary

A paid SDK can still be inspected and modified by the person using it. A check like if (payload.plan === "pro") return true is easy to locate and patch. Hiding that line behind obfuscation changes how hard it is to find - not the underlying trust model. The customer controls the environment where the code runs.

Client-side checks can still be useful for product behavior, like gating UI features or preventing requests you don't intend to make. But enforce access to valuable backend resources on the server. Validate entitlements server-side when a request needs to grant access to hosted functionality, data, or computation. That approach costs more to operate and requires network access, so it's not a fit for every offline library.

Also: don't use obfuscation to hide behavior users would consider unfair or unsafe. Document network requests and license requirements. Don't "protect" the SDK by disguising malware, unexpected data collection, or behavior that conflicts with registry rules. If your library is open source, readable output is usually the right default; protection tends to make the most sense when the implementation is part of a commercial product.

How do you verify a package before publishing it?

Consumers install the release artifact. A tidy-looking source tree and a green local build aren't enough. You want checks against the packed package, because that's where allowlist mistakes, leftover maps, and wrong entry points show up.

First, do a dry run and review the file list:

npm pack --dry-run

Confirm the archive contains the JavaScript and declaration files you expect, plus the README and license. Stop if you see source files, tests, internal documentation, example environment files, debug bundles, or map files that shouldn't be public. If you want to inspect the actual tarball, run npm pack and inspect it with your usual archive tooling.

Next, verify that the metadata points where you think it points. The declaration-related fields should resolve to .d.ts files. Runtime entry points should resolve to the compiled formats you intend to support. If you support both ESM and CommonJS, test both paths - don't assume the manifest is correct just because it parses.

Test the artifact consumers will install

Install the packed archive into a clean test project (or use an equivalent package-level test setup). Check that TypeScript can resolve your public declarations and that representative calls produce the expected type errors and autocomplete behavior.

Then run runtime tests against the same minified/protected build you'll publish. Testing only the unprotected development output misses the riskiest differences - exactly the ones introduced by your publish-time transformations.

A practical release checklist:

  • The package archive contains no src/, test fixtures, or unintended internal files.
  • It contains no .map or .d.ts.map files when those aren't meant to be public.
  • No published JavaScript file points to a source map that should stay private.
  • types and exports.types resolve to the intended declarations.
  • main, module, and conditional exports resolve to the runtime files you tested.
  • Tests run against the packed, minified, or protected artifact - not just your repository build.
  • License checks don't depend on a single client-side branch for important access control.
  • Documentation accurately describes licensing and network behavior.

Key takeaways

  • Types are the contract; JavaScript is the implementation. Publish declarations for editor support without shipping your TypeScript source tree.
  • Packaging choices matter more than obfuscation for accidental leaks. An allowlist and archive review catch common mistakes.
  • Maps can undo source hiding. Disable them in the publish build (or keep them in private debug artifacts).
  • Client-side code is inspectable. Use protection to raise copying costs, not to pretend secrecy replaces server-side authorization.
  • Test what you ship. The packed artifact - not your repo - is what consumers actually receive.

Conclusion

A well-packaged TypeScript library can deliver great editor support without shipping your source tree. Publish clear declarations, point runtime entry points at compiled JavaScript, disable maps you don't mean to share, and use an allowlist to control what lands in the archive. Then inspect and test the exact package consumers will install.

If the implementation is commercially valuable, minification or selective protection can make casual copying harder. Just be explicit about the limits: if the code runs on a customer's machine, it can be inspected. Your release decision should come down to the value of the logic, the cost of added complexity, and where you can enforce access reliably - not on promises of perfect secrecy.

Frequently Asked Questions

Yes. Consumers can read the JavaScript they install, call its exported functions, and observe inputs and outputs. Minification or obfuscation can make analysis less convenient, but neither makes client-side code secret. Treat these techniques as friction against casual copying, not as a guarantee against reverse engineering.
Usually, yes. Declaration files give consumers autocomplete, compile-time checks, and a documented API surface without including function bodies. Ensure the `types` and `exports.types` entries point to the intended declarations, and avoid exporting internal types that reveal details consumers do not need.
They can be, but they may expose more than you intend. JavaScript maps can contain original source text or point to published source files. Declaration maps can guide editors from a declaration back to TypeScript. If you want to keep implementation files private, disable maps for the published build and inspect the package archive.
Not reliably on its own. A consumer controls the machine running client-side code and can inspect or modify it. Obfuscation can increase the work required to find a check, but valuable backend access should be authorized server-side. For offline products, weigh the limits of client-side enforcement against the product’s requirements.

Related articles