Output-Based Questions
TL;DR
Output-based questions provide a snippet of TypeScript code and ask you: “What does this compile to?”, “Does this throw an error?”, or “What is the inferred type of X?”.
Question 1: Excess Properties
interface Point { x: number; y: number; }
function logPoint(p: Point) { console.log(p.x, p.y); }
// Scenario A
const obj = { x: 1, y: 2, z: 3 };
logPoint(obj);
// Scenario B
logPoint({ x: 1, y: 2, z: 3 });
Answer: Scenario A compiles perfectly because of Structural Typing (obj has x and y). Scenario B throws a compile error: “Object literal may only specify known properties, and ‘z’ does not exist”. TS is stricter with inline object literals to catch typos.
Question 2: The never Assignment
function throwError(): never {
throw new Error("Boom");
}
let a: number = throwError();
let b: string = throwError();
Answer: Both a and b compile perfectly! never is the “bottom type” in TypeScript. It is assignable to every other type. However, nothing (not even any) can be assigned to never.
Question 3: Distributive Conditionals
type ToArray<T> = T extends any ? T[] : never;
type MyType = ToArray<string | number>;
Answer: MyType evaluates to string[] | number[]. Because the conditional type is acting on a bare generic T, it automatically distributes across the union. (It does NOT evaluate to (string | number)[]).
Question 4: Const Assertions
const config = {
method: "GET",
url: "https://api.com"
} as const;
config.method = "POST";
Answer: Compile Error! The as const assertion recursively adds the readonly modifier to all properties and narrows the type of method from string to the exact literal "GET".
Question 5: Optional Chaining with Non-Null Assertion
interface User {
profile?: { name: string };
}
const user: User = {};
console.log(user.profile!.name);
Answer: This compiles perfectly in TS, but crashes at runtime! The ! (Non-Null Assertion) tells the TS compiler to ignore the undefined possibility. But since user.profile is actually undefined at runtime, trying to access .name throws a TypeError: Cannot read properties of undefined. This highlights why ! is dangerous.
Question 6: Type Widening inside Callbacks
let value: string | number = "hello";
if (typeof value === "string") {
setTimeout(() => {
console.log(value.toUpperCase());
}, 100);
}
Answer: Compile Error! Inside the if block, value is narrowed to string. But inside the setTimeout callback, TS resets the narrowing back to string | number because it cannot guarantee that value wasn’t reassigned by other code before the timeout executed.