typescriptadvanced
TypeScript Advanced Quiz
Generics, conditional types, mapped types, utility types, and inference.
7 questions
By EZ4Code Team
1. What does this generic function signature mean?
function identity<T>(arg: T): T {
return arg;
}T must be a specific type
T is a type parameter — the function works with any type and preserves it
T stands for "Tuple"
T can only be a string
Explanation: `<T>` declares a type parameter. The function accepts an argument of type `T` and returns type `T`. When you call `identity<string>("hi")`, `T` is `string`. When you call `identity(42)`, TypeScript infers `T` as `number`. The actual type is preserved through the function.
2. What does `Partial<T>` do?
Removes some properties from T
Makes all properties of T optional
Makes all properties required
Returns half of T
Explanation: `Partial<T>` is a built-in utility type that makes every property of `T` optional. It's equivalent to a mapped type `{ [K in keyof T]?: T[K] }`. Useful for update/patch operations where you only provide some fields.
3. What does `keyof T` produce?
interface User { id: number; name: string; }
type Keys = keyof User;The values of User
A union of "id" | "name"
The type User itself
An array of keys
Explanation: `keyof T` produces a union of the property names of `T`. For `User`, that's `"id" | "name"`. It's commonly used in generic constraints like `<K extends keyof T>`.
4. What does this conditional type evaluate to?
type IsString<T> = T extends string ? "yes" : "no";
type A = IsString<"hello">;
type B = IsString<42>;A = "yes", B = "yes"
A = "yes", B = "no"
A = "no", B = "yes"
Both are errors
Explanation: Conditional types use `T extends U ? X : Y` syntax. `"hello"` extends `string`, so `A` is `"yes"`. `42` does not extend `string`, so `B` is `"no"`. This is evaluated at the type level, not runtime.
5. What does `Pick<T, K>` do?
interface User { id: number; name: string; email: string; }
type UserPreview = Pick<User, "id" | "name">;Creates a type with only id and name
Removes id and name from User
Makes id and name optional
Creates a type with all properties except id and name
Explanation: `Pick<T, K>` constructs a type by picking only the properties listed in `K`. So `UserPreview` has only `id` and `name`. The opposite is `Omit<T, K>` which removes the listed properties.
6. What is the difference between `any` and `unknown`?
They are identical
`any` allows any operation; `unknown` requires type narrowing first
`unknown` allows any operation; `any` requires narrowing
`any` is for objects, `unknown` is for primitives
Explanation: Both accept any value, but `any` lets you do anything without checks (unsafe), while `unknown` forces you to narrow the type (via `typeof`, `instanceof`, or type guards) before using it. `unknown` is the type-safe alternative to `any`.
7. What does `readonly` do?
interface Config {
readonly apiKey: string;
}Makes apiKey a constant at runtime
Prevents reassignment of apiKey at compile time
Makes apiKey private
Makes apiKey static
Explanation: `readonly` is a compile-time check — TypeScript will error if you try to reassign `config.apiKey = ...`. At runtime, JavaScript has no protection, so the property is still mutable. For deep immutability, use `Readonly<T>` or `as const`.