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 THIS TESTS: 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.
A GOOD ANSWER COVERS: 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.
COMMON WRONG ANSWERS: 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.
LIKELY FOLLOW-UPS: 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.
ONE 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.
Read the original → typescriptlang.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.