0
0
Angularframework~20 mins

Type annotations in components in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Type Annotation Mastery in Angular
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Angular component with typed input?

Consider this Angular standalone component using type annotations for its input property. What will be rendered in the browser?

Angular
import { Component, Input } from '@angular/core';

@Component({
  selector: 'app-greet',
  standalone: true,
  template: `<p>Hello, {{ name.toUpperCase() }}!</p>`
})
export class GreetComponent {
  @Input() name!: string;
}
A<p>Hello, WORLD!</p>
B<p>Hello, world!</p>
CRuntime error: Cannot read property 'toUpperCase' of undefined
DCompilation error: Property 'name' is not initialized
Attempts:
2 left
💡 Hint

Think about how Angular initializes inputs and the effect of the ! operator.

📝 Syntax
intermediate
1:30remaining
Which option correctly types a component property as a number array?

In Angular, you want to declare a component property scores as an array of numbers. Which option is syntactically correct?

Ascores: number = [10, 20, 30];
Bscores: Array = [10, 20, 30];
Cscores: [number] = [10, 20, 30];
Dscores: number[] = [10, 20, 30];
Attempts:
2 left
💡 Hint

Remember the TypeScript syntax for arrays.

🔧 Debug
advanced
2:00remaining
What error does this Angular component produce due to incorrect type annotation?

Analyze the following Angular component code. What error will occur when compiling or running it?

Angular
import { Component, Input } from '@angular/core';

@Component({
  selector: 'app-counter',
  standalone: true,
  template: `<p>Count: {{ count }}</p>`
})
export class CounterComponent {
  @Input() count: string = 0;
}
ATemplate error: Interpolation expects a number but got string.
BTypeScript error: Type 'number' is not assignable to type 'string'.
CRuntime error: Cannot read property 'count' of undefined.
DNo error; component works fine.
Attempts:
2 left
💡 Hint

Check the type of count and its assigned value.

lifecycle
advanced
2:00remaining
How does TypeScript type annotation affect Angular component lifecycle hooks?

Given this Angular component with a typed property and ngOnInit lifecycle hook, what will be the value of message after initialization?

Angular
import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-msg',
  standalone: true,
  template: `<p>{{ message }}</p>`
})
export class MsgComponent implements OnInit {
  message!: string;

  ngOnInit() {
    this.message = 'Hello from ngOnInit';
  }
}
ACompilation error: Property 'message' is not initialized
BThe template displays: undefined
CThe template displays: Hello from ngOnInit
DRuntime error: Cannot read property 'message' of undefined
Attempts:
2 left
💡 Hint

Consider the effect of the ! operator and when ngOnInit runs.

🧠 Conceptual
expert
2:30remaining
Which statement about type annotations in Angular components is TRUE?

Choose the correct statement about how TypeScript type annotations affect Angular component behavior and compilation.

AType annotations help catch errors at compile time but do not affect runtime behavior.
BType annotations enforce runtime type checks in Angular templates.
CAngular ignores all TypeScript types and compiles components without any checks.
DType annotations automatically convert input values to the declared types at runtime.
Attempts:
2 left
💡 Hint

Think about when TypeScript types are checked versus Angular template rendering.