Basic Types, Inference and Annotations — Concept Check
Questions on Basic Types, Inference and Annotations. Annotate function boundaries and let inference handle the rest. Over-annotating is a common beginner habit that makes code noisier without making it safer.
Questions in this quiz
8 questions on Basic Types, Inference and Annotations — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A developer writes `const x: number = 10;`. What is the issue with this annotation?
- AThe annotation is necessary for clarity.
- BThe type should be `bigint` for large numbers.
- CIt should be `let` instead of `const`.
- DIt's redundant; TypeScript infers `x` as a number.
2.You have a function `function calculate(a: number, b: number): number { return a + b; }`. What would happen if you remove the return type annotation?
- AThe function would still work; TypeScript infers the return type.
- BThe return type would default to `any`.
- CThe function would throw an error due to missing annotation.
- DThe function would return `undefined`.
3.A developer initializes an empty array with `const items: string[] = [];`. Why is this annotation necessary?
- ATo allow the array to hold any type.
- BTo prevent TypeScript from inferring it as `never[]`.
- CTo make it mutable.
- DTo ensure it can only hold numbers.
4.A developer writes `let value: any = getValue(); value.doSomething();`. What is a potential risk here?
- A`value` is guaranteed to have `doSomething`.
- BThe code will not compile due to type errors.
- C`value` might not have `doSomething`, leading to runtime errors.
- D`value` will always be `undefined`.
5.Why might a developer choose `unknown` over `any` for a variable type?
- A`unknown` allows any operation without errors.
- B`unknown` is faster at runtime.
- C`unknown` requires type narrowing before use, improving safety.
- D`unknown` is the default type in TypeScript.
6.A developer writes `const dir = 'up'; let dir2 = 'up';`. What is the difference in their types?
- ABoth are of type `string`.
- B`dir` is of type `'up'`, while `dir2` is of type `string`.
- C`dir` is mutable, `dir2` is not.
- D`dir` is of type `string`, while `dir2` is of type `'up'`.
7.A developer writes `function handler(e: Event): void { return true; }`. What is the issue here?
- AThe type `Event` is incorrect for `e`.
- BThe function should return `false`.
- CThe return type `void` means the return value is ignored.
- DThe function should not return anything.
8.A developer uses `type Comparator = (a: Problem, b: Problem) => number;` and assigns a function without parameter types. Why is this valid?
- AParameter types are inferred from `Comparator`.
- BThe function will not compile without explicit types.
- CThe function defaults to `any` type.
- DTypeScript ignores parameter types.