Recall & Review
beginner
What is an index signature in TypeScript?
An index signature allows you to define the type of keys and values for objects with dynamic keys, meaning keys that are not known in advance.
Click to reveal answer
beginner
How do you declare an index signature for an object with string keys and number values?
You write:
{ [key: string]: number } which means any string key maps to a number value.Click to reveal answer
intermediate
Can index signatures use types other than string for keys?
Yes, index signatures can use
string or number as key types, but not other types like boolean.Click to reveal answer
intermediate
What happens if you mix explicit properties and an index signature in a TypeScript interface?
Explicit properties must have types compatible with the index signature's value type, otherwise TypeScript will show an error.
Click to reveal answer
beginner
Why are index signatures useful in real-life programming?
They let you work with objects where keys are not fixed, like dictionaries or maps, making your code flexible and type-safe.Click to reveal answer
Which syntax correctly declares an index signature for an object with string keys and boolean values?
✗ Incorrect
Option D correctly uses string keys and boolean values in the index signature.
What type can NOT be used as an index signature key in TypeScript?
✗ Incorrect
Only string and number types can be used as index signature keys, boolean is not allowed.
If an interface has an index signature
{ [key: string]: number }, what type must explicit properties have?✗ Incorrect
Explicit properties must be compatible with the index signature value type, which is number here.
What does this index signature mean?
{ [id: number]: string }✗ Incorrect
The syntax means keys are numbers and values are strings.
Why use index signatures instead of fixed property names?
✗ Incorrect
Index signatures allow objects to have keys that are not known ahead of time.
Explain what an index signature is and how it helps with dynamic keys in TypeScript.
Think about objects where keys are not fixed but you still want to know the type of values.
You got /3 concepts.
Describe the rules for mixing explicit properties and index signatures in a TypeScript interface.
Consider what happens if explicit property types don't match the index signature type.
You got /3 concepts.