Advanced Types in TypeScript Quiz

This quiz explores Advanced Types in TypeScript, including topics such as type assertions, type guards, conditional and mapped types, template literal types, recursive types, and utility types. It aims to provide a deeper understanding of TypeScript’s powerful type system and its advanced features for creating robust and scalable applications.

Last Updated :
Discuss
Comments

Question 1

What does the following syntax represent in TypeScript?

TypeScript
let value: unknown = "Hello";
let strValue = value as string;


  • value is cast to string using a type guard.

  • value is asserted to be of type string.

  • value is converted to a string at runtime.

  • value is conditionally mapped to a string.

Question 2

What does the following syntax represent in TypeScript?

TypeScript
let value: unknown = "Hello";
let strValue = value as string;


  • It defines a mapped type.

  • It defines a conditional type.

  • It defines a custom type assertion.

  • It defines a type guard for string.

Question 3

What is the purpose of conditional types in TypeScript?

  • To conditionally assign types at runtime.

  • To allow dynamic type conversions.

  • To create types based on a condition evaluated at compile time.

  • To replace union types with intersection types.

Question 4

What does the following mapped type do?

TypeScript
type ReadonlyProperties<T> = {
    readonly [K in keyof T]: T[K];
};


  • Converts all keys of T to strings.

  • Makes all properties of T optional.

  • Makes all properties of T readonly.

  • Maps all values of T to undefined.

Question 5

What is a template literal type in TypeScript?

  • A type that allows using variables in strings.

  • A type that constructs string literals using embedded expressions.

  • A runtime feature for combining strings.

  • A way to define constant string unions.

Question 6

What does the following template literal type produce?

TypeScript
type Status = "loading" | "success" | "error";
type StatusMessage = `Status: ${Status}`;


  • A union of string literals prefixed with "Status.

  • A dynamic string generator.

  • A type for objects with a Status property.

  • A function type for status messages.

Question 7

What is the purpose of recursive types in TypeScript?

  • To define self-referencing type structures.

  • To prevent infinite type resolution.

  • To map all properties to optional.

  • To iterate over union types.

Question 8

Which of the following defines a recursive type for a nested object structure?

TypeScript
type NestedObject = {
    value: string;
    children?: NestedObject[];
};


  • A type with a string value and optional array of NestedObject children.

  • A type that prevents nested arrays.

  • A type that enforces all properties to be strings.

  • A mapped type for optional properties.

Question 9

What is the purpose of the Pick<T, K> utility type in TypeScript?

  • To exclude properties K from type T.

  • To create a new type with only properties K from T.

  • To make all properties of T readonly.

  • To create an intersection type with T and K.

Question 10

How does the Record<K, V> utility type differ from Partial<T>?

  • Record<K, V> defines a type with keys of type K and values of type V, while Partial<T> makes all properties of T optional

  • Record<K, V> excludes properties, while Partial<T> includes properties.

  • Partial<T> applies to strings only, while Record<K, V> applies to numbers only.

  • Record<K, V> prevents the use of generic types, while Partial<T> allows them.

There are 10 questions to complete.

Take a part in the ongoing discussion