Create a Person class using TypeScript parameter properties
This tests TypeScript parameter properties: constructor access modifiers auto-declare and initialize fields. A strong answer gives class Person { constructor(public name: string, private age: number) {} } and notes it removes manual this.name = name…
What's really being asked
TypeScript parameter properties, a syntax shortcut where placing an access modifier such as public or private directly on a constructor parameter simultaneously declares a class field with that name and assigns the incoming argument to it. The question also checks whether you understand visibility modifiers and can recognize when manual field declarations and constructor assignments are unnecessary boilerplate.
The full answer
First, write the exact class declaration: class Person { constructor(public name: string, private age: number) {} }. Second, explain that prefixing a constructor parameter with an access modifier tells TypeScript to automatically generate the property and perform the assignment, so you do not need to declare name: string or age: number at the top of the class and you do not need this.name = name inside the constructor body. Third, clarify that public makes name readable and writable from outside the instance, whereas private limits age to code inside the class. Fourth, mention that strictPropertyInitialization is satisfied because the constructor handles initialization at instantiation time, which is equivalent to giving a field an initializer.
The mistakes people make
The biggest red flag is manually declaring fields name: string and age: number and then writing this.name = name and this.age = age inside the constructor body. This proves you do not know the shorthand and adds four unnecessary lines. Another mistake is omitting the type annotations on the parameters, which causes TypeScript to infer implicit any and weakens type safety. A third error is forgetting that fields declared without initializers will trigger strictPropertyInitialization errors unless they are definitely assigned in the constructor, which is why the shorthand is especially convenient.
What usually comes next
How does this compile to JavaScript? The emitted code moves assignments into the constructor body and strips access modifiers because they are a TypeScript-specific layer. What does readonly do here? It produces a property assignable only during construction, consistent with readonly field behavior. Can you mix parameter properties with ordinary parameters? Yes, only parameters with an access modifier become class properties. What about default values? Supply them directly in the parameter list like any function parameter.
A concrete example
class Person { constructor(public name: string, private age: number) {} greet() { console.log('Hi, I am ' + this.name); } getAge() { return this.age; } } const p = new Person('Ada', 30); console.log(p.name); this works because name is public. console.log(p.age); this produces a compile-time error because age is private and cannot be accessed from outside the class.
Interview question
When defining a TypeScript class, which outcome occurs when you write constructor(public name: string) instead of declaring a field and assigning it manually?
- a.The public keyword is stripped during compilation and name remains a local parameter only
- b.You must still write name: string at the top of the class to satisfy strictPropertyInitialization
- c.The name parameter becomes accessible only inside the constructor due to block scoping
- d.TypeScript automatically declares a name property on the class and initializes it from the argumentCorrect
Why? this is the answer
Prefixing a constructor parameter with an access modifier tells TypeScript to generate the property declaration and assignment automatically, removing the need for manual boilerplate. The distractor about still writing name: string reflects the common misconception that parameter properties require explicit field declarations, which they do not.
Just read this? Test yourself on what you have been reading.
Read the original → typescriptlang.org
- #typescript
- #classes
- #constructors
- #parameter properties
- #visibility modifiers
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on typescript — each one lists the topics its interview covers.
See open roles