[{"id":"typescriptadvanced-001","courseId":"typescript-advanced","domain":"types-and-values","topic":"Variadic tuple types","prompt":"partialCall takes a function and its first arguments, then returns a function for the rest. Given const foo = (x: string, y: number, z: boolean) => {}, what type does partialCall(foo, 'hello') return in TypeScript 4.0 or later?","hint":"Work out which leading arguments are consumed and what the variadic tuple leaves over.","explain":"A variadic tuple type like [...T, ...U] splits one parameter list into a leading part and a trailing part. Supplied arguments match the leading part; the trailing part is what the new function still needs.","source":"https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-0.html","interaction":{"type":"single"},"options":[{"id":"a","text":"(y: number, z: boolean) => void","why":"The head argument 'hello' fills x, so the returned function takes the leftover y and z."},{"id":"b","text":"(x: string, y: number, z: boolean) => void","whyNot":"The head argument already supplied x, so the returned function no longer accepts it."},{"id":"c","text":"(...tailArgs: unknown[]) => void","whyNot":"The leftover parameter types are kept in the tuple, so they are not widened to unknown."},{"id":"d","text":"(y: number, z: boolean, ...rest: string[]) => void","whyNot":"The returned function takes exactly the leftover tuple; no extra rest parameter is added."}],"answer":"a","explanation":"In TypeScript 4.0, variadic tuple types such as [...T, ...U] let inference match a leading prefix of a function's parameters to the supplied arguments and type the remaining parameters of the returned function.","concept":"In TypeScript 4.0, variadic tuple types such as [...T, ...U] let inference match a leading prefix of a function's parameters to the supplied arguments and type the remaining parameters of the returned function.","rev":1,"kind":"scenario","code":"declare function partialCall<\n  T extends unknown[],\n  U extends unknown[],\n  R\n>(\n  f: (...args: [...T, ...U]) => R,\n  ...headArgs: T\n): (...tailArgs: U) => R","takeaways":["Variadic tuples let a wrapper type the leftover parameters of the function it wraps."]},{"id":"typescriptadvanced-002","courseId":"typescript-advanced","domain":"types-and-values","topic":"Labeled tuple elements","prompt":"In TypeScript 4.0 or later, this tuple type is valid, because only its first element needs a label.","hint":"Check whether labeled and unlabeled elements may be mixed within one tuple type.","explain":"Tuple elements can be named the way parameters are, purely for documentation and editor tooling. Naming does not change how values are checked, but the naming rules apply to the tuple as a whole.","source":"https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-0.html","interaction":{"type":"true-false"},"options":[{"id":"a","text":"True","whyNot":"Once one element is labeled, every element must be labeled, so the unlabeled number makes this an error."},{"id":"b","text":"False","why":"The unlabeled number is the problem: a labeled tuple must label all of its elements."}],"answer":"b","explanation":"In TypeScript 4.0, tuple element labels are optional for documentation, but if any element is labeled, all elements in that tuple must be labeled.","concept":"In TypeScript 4.0, tuple element labels are optional for documentation, but if any element is labeled, all elements in that tuple must be labeled.","rev":1,"kind":"scenario","code":"type Bar = [first: string, number];","takeaways":["Label every element of a tuple, or none of them."]},{"id":"typescriptadvanced-003","courseId":"typescript-advanced","domain":"types-and-values","topic":"Enum member literal types","prompt":"Enum typing in TypeScript changed over several releases. Put these stages of how enum members were typed in the order they happened, earliest first.","hint":"Start from the original design, then follow when literal types were introduced.","explain":"Enum typing has changed across several releases, and each change aimed at a limit of the design before it. Consider what a member's type was tied to, and what happened when a member's value could not be computed.","source":"https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-0.html","interaction":{"type":"sort","items":[{"text":"Enums are plain numeric constants whose members share one type.","why":"This was the original design: members were just numbers, with no type of their own."},{"text":"Enum literal types give each member its own type, and the enum becomes a union of them.","why":"TypeScript 2.0 added this, so members could be referred to and narrowed as types."},{"text":"A computed member makes TypeScript quietly drop the enum's literal types.","why":"Members such as Math.random() broke that link, so TypeScript fell back to the old strategy."},{"text":"Every enum becomes a union enum, with a unique type for each computed member.","why":"TypeScript 5.0 made this the rule, so every enum can be narrowed and used as types."}]},"options":[],"answer":"o:abcd","explanation":"Enums began as numeric constants sharing one type; TypeScript 2.0 added enum literal types, and TypeScript 5.0 made every enum a union enum, including computed members.","concept":"Enums began as numeric constants sharing one type; TypeScript 2.0 added enum literal types, and TypeScript 5.0 made every enum a union enum, including computed members.","rev":1,"kind":"scenario","takeaways":["Since TypeScript 5.0, every enum is a union enum, even with computed members."]},{"id":"typescriptadvanced-004","courseId":"typescript-advanced","domain":"types-and-values","topic":"const enum inlining","prompt":"In TypeScript, a const enum is compiled differently from a regular enum. Which TWO statements about const enums are true?","hint":"Ask what happens to the enum declaration itself and to each place the enum is used.","explain":"A regular enum compiles into an object that code can read at runtime. A const enum is handled at compile time instead, so the compiler decides what each use site looks like and what output remains.","source":"https://www.typescriptlang.org/docs/handbook/enums.html","interaction":{"type":"multiple","tellCount":true},"options":[{"id":"a","text":"Each use of a const enum member is replaced by its value in the emitted JavaScript.","why":"Const enum members are inlined at each use site, so no property lookup remains in the output."},{"id":"b","text":"The Direction declaration is removed from the emitted JavaScript entirely.","why":"Const enums are completely removed during compilation, so no enum object exists at runtime."},{"id":"c","text":"The emitted JavaScript keeps an object with forward and reverse mappings.","whyNot":"That is the output for a regular enum; a const enum leaves no object behind at runtime."},{"id":"d","text":"Const enum members may be computed, for example Up = Math.random().","whyNot":"Const enums can only use constant expressions, so computed members are not allowed."},{"id":"e","text":"A library that publishes ambient const enums works fine with isolatedModules consumers.","whyNot":"Ambient const enums are incompatible with isolatedModules, so consumers cannot use both."},{"id":"f","text":"Setting preserveConstEnums stops the project's own const enums from being inlined.","whyNot":"preserveConstEnums only emits the object like a plain enum; the project can still inline its own enums."}],"answer":"ab","explanation":"Const enums are removed at compile time and their members are inlined at each use site; they cannot have computed members.","concept":"Const enums are removed at compile time and their members are inlined at each use site; they cannot have computed members.","rev":1,"kind":"scenario","code":"const enum Direction {\n  Up,\n  Down,\n}\nlet d = Direction.Up;","takeaways":["Const enum values are copied into the code that uses them, and no enum object is emitted."]},{"id":"typescriptadvanced-005","courseId":"typescript-advanced","domain":"types-and-values","topic":"never assignability","prompt":"With fail declared as returning never, which statement fails to compile in TypeScript?","hint":"Pay attention to which side of the assignment is never.","explain":"The type never has no values, so an expression of type never can appear wherever a value is expected. Assignments follow a direction, and the outcome depends on which side of the = holds never.","source":"https://www.typescriptlang.org/docs/handbook/type-compatibility.html","interaction":{"type":"single"},"options":[{"id":"a","text":"let s: string = fail();","whyNot":"never is assignable to every type, so this compiles."},{"id":"b","text":"let u: unknown = fail();","whyNot":"never is assignable to unknown too, since everything is assignable to unknown."},{"id":"c","text":"let x: never = 'hi';","why":"Nothing except never is assignable to never, so a string value is rejected."},{"id":"d","text":"let o: object = fail();","whyNot":"never is assignable to object, like every other type."}],"answer":"c","explanation":"never is assignable to every type, but no type other than never is assignable to never.","concept":"never is assignable to every type, but no type other than never is assignable to never.","rev":1,"kind":"scenario","code":"declare function fail(): never;","takeaways":["A never-returning call can stand in for any type, but no value can be stored into a never variable."]}]