Advanced TypeScript test (Advanced)
40 questions · 40 minutes · ₹6
Conditional, mapped and template literal types; type-level programming.
Take the Advanced TypeScript test
What it covers
- Conditional types and infer
- Mapped and template literal types
- Variance and type compatibility
- Decorators
- Library typing patterns
Interview format
- Questions: 40 questions — 24 multiple choice, 12 multi-answer, 4 predict-the-output
- Time: 40 minutes, timed from the first question
- Difficulty: Advanced
- Skill area: Frontend
- Price: ₹6 a test
Earn a certificate
The Advanced TypeScript test is one of the 7 tests of the Frontend Developer: Advanced learning path. Pass it and the other 6 tests at 70% or more and readyforhires issues you a verifiable certificate in your name, with its own code and verify link to share or add to LinkedIn.
Sample questions
5 sample questions from the Advanced TypeScript test, with the answers. Samples are taken out of the live test, so you will not meet these when you sit it.
Which of the following statements best describes the difference between standard mapped types and mapped types with key remapping in TypeScript?
- Mapped types with key remapping are only compatible with union types, not intersection types.
- Key remapping in mapped types only applies to string keys, not number or symbol keys.
- Mapped types with key remapping are more efficient than standard mapped types for large object types.
- Remapped keys are always derived from the original key set, while mapped type with key remapping allows for more flexibility in transforming keys based on a condition or a function.
Answer: D. Remapped keys are always derived from the original key set, while mapped type with key remapping allows for more flexibility in transforming keys based on a condition or a function.
Mapped types with key remapping allow you to apply a transformation function to the keys of an object type, enabling you to create new keys based on the original ones. This is useful when you need to derive new keys from existing ones in a structured way. In contrast, a mapped type without key remapping simply applies the same transformation to all keys, which is less flexible. The correct answer highlights the distinction and use cases for each approach.
What happens when a decorator is applied to an accessor (getter or setter) in TypeScript?
- The decorator receives the accessor's name and can rename it.
- The decorator receives `undefined` and can return an initializer function.
- The decorator is not supported on accessors in TypeScript.
- The decorator receives the current value of the accessor and can modify it.
Answer: B. The decorator receives `undefined` and can return an initializer function.
When a decorator is applied to an accessor (such as a getter or setter), it receives `undefined` as the value. This is because the value of the accessor is not yet initialized at the time the decorator is called. However, the decorator can return an initializer function to set the initial value of the accessor.
Which of the following statements accurately describes how conditional types are supported in TypeScript?
- TypeScript does not support conditional types and relies solely on type guards and type narrowing for similar functionality.
- TypeScript provides support for conditional types through the use of the 'extends' keyword and the 'infer' keyword for type inference within conditional type expressions.
- Conditional types in TypeScript are defined using the 'if' and 'else' keywords, similar to traditional conditional statements in JavaScript.
- Conditional types in TypeScript are only available in strict mode and require the use of the '--experimental' compiler flag.
Answer: B. TypeScript provides support for conditional types through the use of the 'extends' keyword and the 'infer' keyword for type inference within conditional type expressions.
Conditional types in TypeScript allow for type checking and inference based on certain conditions. The 'extends' keyword is used to define a conditional relationship between types, and the 'infer' keyword is used to capture a type that is part of a conditional type expression. This enables advanced type manipulation and inference scenarios.
Which of the following statements accurately describes the behavior of class field decorators in TypeScript?
- Class field decorators are only useful for logging and cannot modify or observe class properties.
- Class field decorators allow you to modify or observe a class property during its initialization, but they cannot be used to add new properties to a class dynamically.
- Class field decorators are not supported in TypeScript and must be implemented manually.
- Class field decorators can be used to add new properties to a class dynamically, just like class decorators.
Answer: B. Class field decorators allow you to modify or observe a class property during its initialization, but they cannot be used to add new properties to a class dynamically.
Class field decorators are applied to class properties and can be used to modify or observe the property during initialization. However, they are not suitable for dynamically adding new properties to a class at runtime. In contrast, decorators applied to the class itself (class decorators) can be used to add new properties or modify the class structure dynamically.
What behavior is expected when augmenting a type in a library using TypeScript?
- The type is augmented with new properties, and existing properties are also overwritten.
- The type is augmented with new properties, and existing properties are not modified.
- The type is completely replaced with a new type definition.
- The type is modified by removing existing properties to make space for new ones.
Answer: B. The type is augmented with new properties, and existing properties are not modified.
When augmenting a type in a library, the correct approach is to add new properties without modifying existing ones. This ensures backward compatibility and avoids breaking changes. Adding new properties allows the type to evolve while maintaining the integrity of existing code that depends on the original type.
Frequently asked questions
- How many questions are in the Advanced TypeScript test?
- 40 questions (24 multiple choice, 12 multi-answer, 4 predict-the-output), with 40 minutes to answer them.
- What does the Advanced TypeScript test cover?
- Conditional types and infer, Mapped and template literal types, Variance and type compatibility, Decorators, Library typing patterns.
- Is the Advanced TypeScript test free?
- It costs ₹6 a test, paid per test with no subscription. Prices in Indian rupees (INR).
- What level is the Advanced TypeScript test?
- Advanced, in Frontend.