A public field in TypeScript is a class property that is accessible without restriction from any context, including internally within the defining class, within derived subclasses, and externally via instances of the class. By default, all class members in TypeScript are implicitly public unless explicitly restricted using theDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
private or protected access modifiers, or ECMAScript private fields (#).
Syntax and Declaration
Public fields can be declared implicitly by omitting an access modifier, or explicitly using thepublic keyword.
Constructor Parameter Properties
TypeScript provides syntactic sugar for declaring and initializing public fields directly within the constructor signature. By prefixing a constructor parameter with thepublic modifier, the compiler automatically generates the corresponding public field and assigns the argument to it upon instantiation.
Technical Characteristics
- Type Erasure: The
publickeyword is a TypeScript-specific access modifier. It exists strictly at compile-time for structural typing and access control. The keyword is completely erased during the emit phase and does not alter the runtime behavior of the resulting JavaScript. - Definite Assignment: When the
strictPropertyInitializationcompiler option is enabled, public fields must be initialized either at their declaration or synchronously within the constructor. If a field is initialized via indirect means (e.g., a dependency injection framework), the definite assignment assertion operator (!) must be appended to bypass the compiler check.
- JavaScript Emission: The compilation of public fields is dictated by the
targetanduseDefineForClassFieldsflags intsconfig.json.- Legacy Emission: When
useDefineForClassFieldsisfalse, initialized public fields are emitted as standard property assignments (this.fieldName = value;) injected directly into the constructor function. - Standard ECMAScript Emission: When
useDefineForClassFieldsistrueand thetargetis ES2022 or newer, TypeScript emits public fields using standard ECMAScript class field syntax. - Polyfilled Standard Emission: When
useDefineForClassFieldsistruebut thetargetis older than ES2022 (e.g., ES2015), TypeScript does not use class field syntax. Instead, it emitsObject.definePropertycalls inside the constructor to polyfill the standard ECMAScript semantics.
- Legacy Emission: When
- Uninitialized Fields and the
declareModifier: Under standard ECMAScript semantics (useDefineForClassFields: true), uninitialized public fields (e.g.,public name: string;) are emitted and initialized toundefinedon the instance. This behavior can inadvertently overwrite prototype-level getters or setters. To define a field’s type without emitting any runtime initialization code, use thedeclaremodifier.
Master TypeScript with Deep Grasping Methodology!Learn More





