Does TypeScript private hide a @Prop property from StencilJS public API?
0 reputation · 08 Mar 2026, 03:42 UTC
0 reputation · 08 Mar 2026, 03:42 UTC
I am building a StencilJS component and need certain reactive properties to remain internal implementation details. The @Prop decorator exposes properties through the custom element's attribute and property interface, so I want to confirm whether TypeScript's private modifier prevents them from becoming part of the public API at runtime.
StencilJS has no dedicated private prop decorator, and TypeScript access modifiers are erased during compilation. I am targeting StencilJS v4 and want to avoid external scripts mutating internal state through the DOM element.
Does a private @Prop property appear in the generated custom element API or StencilJS documentation? What is the supported pattern for keeping reactive state inaccessible to consumers while still using property change tracking?
35775 reputation · 08 Mar 2026, 11:23 UTC
No. In Stencil v4, TypeScript's private modifier does not hide a @Prop from the public API. Access modifiers are erased during compilation, and Stencil registers props via decorator metadata. A private @Prop remains accessible on the custom element instance and can be read or mutated by external scripts through the DOM property interface.
private @Prop is still present on the custom element instance at runtime.Because TypeScript strips private before emitting JavaScript, Stencil's compiler has no runtime signal to exclude the property. The decorator is the only metadata that survives compilation, so it alone determines the public interface.
Use @State() for internal reactive state that is not part of the public API. Stencil does not expose @State as an attribute and omits it from generated public types.
@State() internalCount = 0;
Use an immutable prop instead of relying on TypeScript visibility:
@Prop({ mutable: false }) config: string;
Because build output can vary by target configuration, verify the actual generated API:
npx stencil build --docs.components.d.ts for the property on the custom element interface.readme.md for a prop entry.This guidance assumes Stencil v4 with a standard TypeScript target. The exact documentation output may differ between output targets; the build-output check above is the authoritative verification. Treat private as a compile-time code-organization aid, not a runtime security boundary.
Use comments to ask for clarification. Post a solution as an answer.
35,775 reputation · 08 Mar 2026, 03:59 UTC
TypeScript's private modifier is erased during compilation, so StencilJS compiles @Prop identically regardless of access level. The framework generates the public API and documentation from @Prop presence alone, meaning a private @Prop still appears in the custom element's property interface and typed definition files. To keep a reactive property truly internal and excluded from the public attribute/property API, use @State instead—Stencil treats @State as framework-managed and omits it from generated docs and attribute reflection. If you need change tracking without public exposure, pair a private field with @Watch or a getter/setter backed by @State. For StencilJS v4, this pattern prevents external scripts from mutating the property via the DOM element while preserving internal reactivity.