Types, generics, narrowing, utility types, config
Practise these as flashcards. One or two lines each; the ones you miss come back first.
Start freeStructural typing means that TypeScript checks type compatibility based on the actual properties and methods present, not on the explicit type or interface name. If two objects have the same shape, they are considered compatible.
I use an interface when describing the shape of an object, especially if I expect to extend it later or allow declaration merging. Interfaces are best for defining contracts for classes or objects.
unknown type represent, and how is it different from any?unknown is a type-safe counterpart to any. You can assign any value to unknown, but you must perform type checks before using it, which helps prevent runtime errors. With any, you lose all type safety.
never type mean in TypeScript?never represents values that never occur, such as the return type of a function that always throws an error or never finishes executing. It's useful for exhaustive checks in switch statements.
I use type guards, such as checking a discriminant property or using typeof or instanceof, to narrow the type. For example, with a union { kind: 'a' } | { kind: 'b' }, I check if (obj.kind === 'a') to safely access properties specific to that variant.
A discriminated union is a union of object types that share a common literal property, usually called kind or type. It allows TypeScript to narrow the type based on the value of that property, making code safer and more readable.
Partial<T> and Omit<T, K>.Partial<T> makes all properties of T optional, which is useful for update patterns. Omit<T, K> creates a type by removing the specified keys K from T, which is helpful when you want to exclude certain properties.
I use the extends keyword to constrain the generic parameter. For example, function foo<T extends { id: number }>(arg: T) ensures that arg must have an id property of type number.
ReturnType<T> utility type?ReturnType<T> extracts the return type of a function type T. It's useful when you want to reuse or reference the output type of a function elsewhere in your codebase.
TypeScript treats them as compatible if their structures match, due to structural typing. It doesn't matter if one is an interface and the other is a type alias; only the shape matters.
tsconfig.json for a new project?I always enable strict for full type safety, set target to the desired ECMAScript version, use module as needed (like commonjs or esnext), enable esModuleInterop for compatibility, and specify outDir for build output.
Record<K, T> work and when would you use it?Record<K, T> creates an object type with keys of type K and values of type T. I use it when I need a dictionary-like object, such as mapping strings to user objects: Record<string, User>.
any caused a production bug, and how you would prevent it.