[{"id":"typescriptintermediate-001","courseId":"typescript-intermediate","domain":"types-and-values","topic":"unknown type","prompt":"A function takes a parameter typed unknown and must call toUpperCase() on it. Put these steps in order, from the first try that fails to the code that compiles.","hint":"Check what TypeScript allows on an unknown value before any check is made.","explain":"An unknown value accepts any input, but TypeScript blocks operations on it. Only a narrowing check, or an assertion, makes a specific type's methods usable.","source":"https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-0.html","interaction":{"type":"sort","items":[{"text":"Call x.toUpperCase() directly on the unknown parameter.","why":"TypeScript rejects this: property access isn't allowed on unknown until the type is narrowed."},{"text":"Wrap the call in if (typeof x === \"string\") { ... }.","why":"A typeof check narrows x, so the compiler knows its type inside the branch."},{"text":"Inside that branch, x is now typed as string.","why":"Narrowing has given x a specific type, so string methods are available there."},{"text":"Call x.toUpperCase() inside the branch.","why":"The method exists on the narrowed type, so this line compiles."}]},"options":[],"answer":"o:abcd","explanation":"An unknown value allows no operations until a check narrows it to a specific type, such as with typeof, after which that type's members are available.","concept":"An unknown value allows no operations until a check narrows it to a specific type, such as with typeof, after which that type's members are available.","rev":1,"kind":"scenario","takeaways":["Narrow an unknown value with typeof or instanceof before calling methods on it."]},{"id":"typescriptintermediate-002","courseId":"typescript-intermediate","domain":"types-and-values","topic":"void return type vs undefined","prompt":"A teammate writes this function, which logs a message and never returns anything. Hovering over the function shows an inferred return type. What is it?","hint":"Think about what TypeScript infers when a function has no return statement with a value.","explain":"TypeScript infers a function's return type from its return statements. A function that returns no explicit value gets a dedicated type, distinct from the JavaScript value undefined.","source":"https://www.typescriptlang.org/docs/handbook/2/functions.html","interaction":{"type":"single"},"options":[{"id":"a","text":"void","why":"A function with no return statements, or none with an explicit value, is inferred as void."},{"id":"b","text":"undefined","whyNot":"The function does return undefined at runtime, but TypeScript does not treat void and undefined as the same type."},{"id":"c","text":"any","whyNot":"any is the opt-out type; TypeScript doesn't fall back to it for a function that simply has no return value."},{"id":"d","text":"never","whyNot":"never is for functions that throw or never finish, not for ones that return normally."}],"answer":"a","explanation":"A function with no return statement, or with only a bare return, is inferred as void. void is not the same type as undefined in TypeScript.","concept":"A function with no return statement, or with only a bare return, is inferred as void. void is not the same type as undefined in TypeScript.","rev":1,"kind":"scenario","code":"function logMessage(msg: string) {\n  console.log(msg);\n}","takeaways":["A function that returns nothing is typed void, not undefined."]},{"id":"typescriptintermediate-003","courseId":"typescript-intermediate","domain":"types-and-values","topic":"Tuple types","prompt":"A teammate models a value as a tuple type. Which TWO of these statements about tuple types are true?","hint":"Think about where optional and rest elements may sit, and what they do to length.","explain":"A tuple fixes how many elements a value has and what type each position holds. Optional and rest elements loosen that count, and the length property reflects the change.","source":"https://www.typescriptlang.org/docs/handbook/2/objects.html","interaction":{"type":"multiple","tellCount":true},"options":[{"id":"a","text":"[number, string?] has a length of type 1 | 2.","why":"An optional element can be omitted, so the length is a union of the possible counts."},{"id":"b","text":"pair[2] on a [string, number] tuple compiles and gives undefined.","whyNot":"TypeScript reports an error: a tuple of length 2 has no element at index 2."},{"id":"c","text":"[number?, string] is valid because optional elements can go anywhere.","whyNot":"Optional tuple elements can only come at the end, so the optional element here is not allowed."},{"id":"d","text":"[...boolean[], string, number] is a valid tuple type.","why":"A rest element may come first, followed by fixed elements, so this shape is allowed."},{"id":"e","text":"The length of [string, ...number[]] is the literal type 2.","whyNot":"A tuple with a rest element has a length of type number, since its count is open-ended."},{"id":"f","text":"A readonly [string, number] tuple lets you assign to pair[0].","whyNot":"Writing to any property of a readonly tuple is not allowed."}],"answer":"ad","explanation":"Optional tuple elements must come last and make length a union of numeric literals. A rest element can appear before fixed elements and makes length number.","concept":"Optional tuple elements must come last and make length a union of numeric literals. A rest element can appear before fixed elements and makes length number.","rev":1,"kind":"scenario","takeaways":["Optional tuple elements go at the end; a rest element makes length number."]},{"id":"typescriptintermediate-004","courseId":"typescript-intermediate","domain":"types-and-values","topic":"readonly array types","prompt":"In TypeScript, assigning a value of type readonly string[] to a variable of type string[] compiles without error.","hint":"Check whether assignability between readonly and regular arrays runs in both directions.","explain":"A readonly array type promises that its contents won't be changed through that reference. Regular arrays can be used where readonly ones are expected, but TypeScript does not treat the relationship as two-way.","source":"https://www.typescriptlang.org/docs/handbook/2/objects.html","interaction":{"type":"true-false"},"options":[{"id":"a","text":"True","whyNot":"TypeScript rejects it: a readonly array is not assignable to the mutable string[] type."},{"id":"b","text":"False","why":"Correct: a readonly string[] is not assignable to string[]. Only the reverse, string[] to readonly string[], is allowed."}],"answer":"b","explanation":"Regular arrays assign to readonly array types, but readonly arrays do not assign back to mutable ones; unlike the readonly property modifier, this check is one-way.","concept":"Regular arrays assign to readonly array types, but readonly arrays do not assign back to mutable ones; unlike the readonly property modifier, this check is one-way.","rev":1,"kind":"scenario","takeaways":["Accept readonly T[] when you won't change the array; return readonly T[] when callers must not change it."]},{"id":"typescriptintermediate-005","courseId":"typescript-intermediate","domain":"types-and-values","topic":"Type assertions with as","prompt":"A teammate adds this line to a .ts file. What happens when the file is compiled?","hint":"Think about how far apart the two types are, and when TypeScript checks an assertion.","explain":"An as assertion tells the compiler to treat a value as another type. The compiler only accepts assertions between types that are related, and the assertion itself is removed from the output.","source":"https://www.typescriptlang.org/docs/handbook/2/everyday-types.html","interaction":{"type":"single"},"options":[{"id":"a","text":"TypeScript reports an error, because string and number do not overlap enough for the assertion.","why":"Assertions to an unrelated type are rejected at compile time, which catches impossible coercions."},{"id":"b","text":"It compiles, and n holds the number NaN at runtime.","whyNot":"An assertion never converts a value; it is only a compile-time statement about its type."},{"id":"c","text":"It compiles silently, because as always forces a conversion.","whyNot":"Not for unrelated types: TypeScript only allows assertions to a more or less specific version of a type."},{"id":"d","text":"It compiles, and the program throws a TypeError when n is read.","whyNot":"The line doesn't compile, and assertions have no runtime check, so no TypeError is thrown."}],"answer":"a","explanation":"Type assertions are checked at compile time only: TypeScript rejects assertions between types that do not overlap, and the assertion adds no runtime check or conversion.","concept":"Type assertions are checked at compile time only: TypeScript rejects assertions between types that do not overlap, and the assertion adds no runtime check or conversion.","rev":1,"kind":"scenario","code":"const n = \"hello\" as number;","takeaways":["An as assertion can't convert a value; use a real conversion when you need one."]}]