[{"id":"typescriptbeginner-001","courseId":"typescript-beginner","domain":"types-and-values","topic":"Type inference from initializers","prompt":"A teammate writes `let city = \"Lisbon\";` with no type annotation. What type does TypeScript give `city`?","hint":"Compare how let and const treat the same starting value.","explain":"With no annotation, TypeScript gives a variable a type based on its starting value. The declaration keyword matters: const and let can give the same value different types.","source":"https://www.typescriptlang.org/docs/handbook/2/basic-types.html","interaction":{"type":"single"},"options":[{"id":"a","text":"string","why":"let lets the value change later, so TypeScript widens the literal to its general type, string."},{"id":"b","text":"\"Lisbon\" (a literal type)","whyNot":"Literal types are kept for const. A let variable can later hold other strings, so it is typed string."},{"id":"c","text":"any","whyNot":"any is the fallback when no type can be inferred, and here the initializer gives one."},{"id":"d","text":"unknown","whyNot":"TypeScript doesn't infer unknown from a value; you have to write it yourself when you want that type."}],"answer":"a","explanation":"With no annotation, let gives a variable the general type of its initializer (string here), while const keeps the narrower literal type.","concept":"With no annotation, let gives a variable the general type of its initializer (string here), while const keeps the narrower literal type.","rev":1,"kind":"scenario","code":"let city = \"Lisbon\";","takeaways":["let widens a literal to its general type; const keeps the literal type."]},{"id":"typescriptbeginner-002","courseId":"typescript-beginner","domain":"types-and-values","topic":"Primitive type names","prompt":"You need type names for a variable that holds text and a variable that holds a count. Which TWO of these are valid TypeScript types for them?","hint":"Use the names that match what typeof reports in JavaScript.","explain":"JavaScript has three everyday primitives. TypeScript's type names for them are the same names that the typeof operator reports.","source":"https://www.typescriptlang.org/docs/handbook/2/everyday-types.html","interaction":{"type":"multiple","tellCount":true},"options":[{"id":"a","text":"string","why":"string is the type for text values such as \"Hello, world\"."},{"id":"b","text":"number","why":"number covers every numeric value, since JavaScript has one number type for both whole and fractional values."},{"id":"c","text":"int","whyNot":"TypeScript has no int type; JavaScript has no integer type, so whole numbers are number too."},{"id":"d","text":"float","whyNot":"There is no float type; fractional values are simply number."},{"id":"e","text":"integer","whyNot":"integer isn't a TypeScript type name; use number for whole numbers."},{"id":"f","text":"double","whyNot":"double is a name from other languages; JavaScript has no separate double, so every numeric value is number."}],"answer":"ab","explanation":"The primitive type names are string, number and boolean, the same names typeof reports; there is no separate int or float type.","concept":"The primitive type names are string, number and boolean, the same names typeof reports; there is no separate int or float type.","rev":1,"kind":"scenario","takeaways":["Use string, number and boolean; TypeScript has no int or float type."]},{"id":"typescriptbeginner-003","courseId":"typescript-beginner","domain":"types-and-values","topic":"Array types","prompt":"A function should take a list of names, where each name is text. Which type annotation describes that list?","hint":"Think about what the brackets do when they come after a type.","explain":"An array type names the type of each item in the list. Brackets written after a type make a list of that type, and similar-looking brackets mean something else.","source":"https://www.typescriptlang.org/docs/handbook/2/everyday-types.html","interaction":{"type":"single"},"options":[{"id":"a","text":"string[]","why":"Brackets after a type make an array of it, so string[] is an array of strings."},{"id":"b","text":"[string]","whyNot":"Brackets around a type are tuple syntax, which is a different thing from an array."},{"id":"c","text":"string","whyNot":"A bare string is a single text value, not a list of them."},{"id":"d","text":"string[][]","whyNot":"Two pairs of brackets make a list of lists, which is more than the names need."}],"answer":"a","explanation":"T[] (the same as Array<T>) is an array of T; [T] with brackets around the type is a tuple, a different type.","concept":"T[] (the same as Array<T>) is an array of T; [T] with brackets around the type is a tuple, a different type.","rev":1,"kind":"scenario","takeaways":["number[] and Array<number> mean the same thing; [number] is a tuple."]},{"id":"typescriptbeginner-004","courseId":"typescript-beginner","domain":"types-and-values","topic":"Union types","prompt":"A function takes `id: number | string`. Which line does TypeScript reject inside the function body?","hint":"A union allows only what every one of its members allows.","explain":"A value typed as a union can be any of its member types. TypeScript allows an operation only if every member supports it, so member-specific features need a check first.","source":"https://www.typescriptlang.org/docs/handbook/2/everyday-types.html","interaction":{"type":"single"},"options":[{"id":"a","text":"id.toUpperCase()","why":"toUpperCase exists only on string, and number has no such method, so the union rejects the call."},{"id":"b","text":"id.toString()","whyNot":"Both string and number have a toString method, so the call is valid for every member."},{"id":"c","text":"console.log(id)","whyNot":"console.log accepts values of any type, so passing either member is fine."},{"id":"d","text":"id === 101","whyNot":"Comparing the union with a number is allowed, because a number is one of its members."}],"answer":"a","explanation":"An operation on a union is allowed only if every member supports it; narrowing with typeof makes member-specific operations legal.","concept":"An operation on a union is allowed only if every member supports it; narrowing with typeof makes member-specific operations legal.","rev":1,"kind":"scenario","takeaways":["A union allows only operations every member supports; narrow with typeof first."]},{"id":"typescriptbeginner-005","courseId":"typescript-beginner","domain":"types-and-values","topic":"Literal types","prompt":"This function takes a union of string literals for align. Which TWO of these calls compile without an error?","hint":"A literal type accepts only the exact values it lists.","explain":"A literal type limits a value to one exact string or number, and a union of literals lists the allowed ones. A variable's declared type decides whether it still counts as a literal.","source":"https://www.typescriptlang.org/docs/handbook/2/everyday-types.html","interaction":{"type":"multiple","tellCount":true},"options":[{"id":"a","text":"printText(\"Hi\", \"left\")","why":"\"left\" is one of the listed values, so the call matches the union."},{"id":"b","text":"printText(\"Hi\", \"right\")","why":"\"right\" is also listed in the union, so this call matches too."},{"id":"c","text":"printText(\"Hi\", \"centre\")","whyNot":"The union lists \"center\"; \"centre\" is a different string and is rejected."},{"id":"d","text":"printText(\"Hi\")","whyNot":"align has no ?, so it is required; leaving the second argument out is a compile error."},{"id":"e","text":"printText(\"Hi\", \"middle\")","whyNot":"Being a string isn't enough; \"middle\" is not one of the listed values."},{"id":"f","text":"let side = \"left\"; printText(\"Hi\", side)","whyNot":"let widens \"left\" to string, and a plain string doesn't fit a union of literals."}],"answer":"ab","explanation":"A union of string literals accepts only those exact values; a let variable holding a literal is typed string and won't fit.","concept":"A union of string literals accepts only those exact values; a let variable holding a literal is typed string and won't fit.","rev":1,"kind":"scenario","code":"function printText(\n  s: string,\n  align: \"left\" | \"right\" | \"center\"\n) {}","takeaways":["Literal unions accept only the exact listed values; let widens them to string."]}]