0
0
Typescriptprogramming~20 mins

Index signatures for dynamic keys in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Index Signature Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this TypeScript code using index signatures?

Consider the following TypeScript interface and object:

interface Scores {
  [player: string]: number;
}

const gameScores: Scores = {
  alice: 10,
  bob: 15
};

console.log(gameScores['alice']);
console.log(gameScores['charlie']);

What will be printed to the console?

Typescript
interface Scores {
  [player: string]: number;
}

const gameScores: Scores = {
  alice: 10,
  bob: 15
};

console.log(gameScores['alice']);
console.log(gameScores['charlie']);
A10 and undefined
B10 and 0
CError at compile time
Dundefined and undefined
Attempts:
2 left
💡 Hint

Think about what happens when you access a key that does not exist in an object.

📝 Syntax
intermediate
1:30remaining
Which option correctly declares an index signature for dynamic keys with string values?

Choose the correct TypeScript syntax for an interface that allows any string key with a string value.

Ainterface Data { key: string; }
Binterface Data { [key]: string; }
Cinterface Data { [key: number]: string; }
Dinterface Data { [key: string]: string; }
Attempts:
2 left
💡 Hint

Index signatures require a type for the key inside square brackets.

🔧 Debug
advanced
2:30remaining
Why does this TypeScript code cause an error?

Given this interface and object:

interface Config {
  [key: string]: number;
  port: number;
}

const serverConfig: Config = {
  port: 8080,
  host: 'localhost'
};

Why does this code cause a TypeScript error?

ABecause 'host' has a string value but the index signature requires number values
BBecause 'port' is missing from the object
CBecause index signatures cannot be combined with explicit properties
DBecause the interface is missing a string index signature
Attempts:
2 left
💡 Hint

Check the types of all properties against the index signature type.

optimization
advanced
2:30remaining
How to optimize a TypeScript interface with index signatures for mixed value types?

You want an interface that allows any string key with either a string or number value. Which is the best way to declare it?

Ainterface Mixed { [key: string]: string & number; }
Binterface Mixed { [key: string]: string | number; }
Cinterface Mixed { [key: string]: any; }
Dinterface Mixed { [key: string]: string; [key: string]: number; }
Attempts:
2 left
💡 Hint

Consider union types for values in index signatures.

🧠 Conceptual
expert
3:00remaining
What is a limitation of using index signatures with dynamic keys in TypeScript?

Which statement best describes a limitation when using index signatures for dynamic keys?

AIndex signatures can only be used with number keys, not string keys
BIndex signatures allow different value types for each key without restriction
CAll explicitly declared properties must conform to the index signature's value type
DYou cannot combine index signatures with any other properties in an interface
Attempts:
2 left
💡 Hint

Think about how explicit properties relate to the index signature type.