Consider this TypeScript code snippet. What will be logged to the console?
interface Person {
name: string;
age: number;
}
const user: Person = { name: "Alice", age: 30 };
console.log(user.name + " is " + user.age + " years old.");Look at how the user object is created and how properties are accessed.
The interface Person defines the shape of the object. The user variable matches this shape. Accessing user.name and user.age prints the expected string.
Which of the following interface declarations correctly defines an interface with an optional property email?
In TypeScript, optional properties use a question mark after the property name.
Option A uses the correct syntax email?: string; to mark email as optional. Other options are invalid syntax.
Examine the following TypeScript code. What error will the compiler report?
interface Product {
id: number;
name: string;
id: string;
}Check if property names can be repeated in an interface.
Interfaces cannot have duplicate property names. Declaring 'id' twice causes a duplicate identifier error.
Given these interfaces, what will happen if you declare a variable of type Combined?
interface A { value: string; }
interface B { value: number; }
interface Combined extends A, B {}Think about how TypeScript handles interface inheritance with conflicting property types.
When interfaces extend others with the same property but different types, TypeScript reports a conflict error because it cannot unify the types.
Consider these two interface declarations in the same scope:
interface Config {
host: string;
}
interface Config {
port: number;
}How many properties does the merged Config interface have?
TypeScript allows interface merging. Properties from both declarations combine.
TypeScript merges interface declarations with the same name by combining their properties. Here, Config has both host and port.