TypeScript Junior
Why would we choose TypeScript over JavaScript for a large-scale enterprise application? What specific 'pain points' of JS does it solve?
Select the correct answer
It executes noticeably faster than JavaScript because types are optimized at runtime.
It removes the need for unit tests by fully guaranteeing program correctness for you.
It lets code run without any build step while still adding strong type guarantees.
It catches type errors at compile time and improves tooling and refactoring.
Explain the difference between explicit and implicit type assignment.
Select the correct answer
Explicit checks types at runtime; implicit checks them only when compiling.
Explicit means you annotate the type; implicit means TypeScript infers it.
Explicit means TypeScript infers the type; implicit means that you annotate it.
Explicit applies only to functions; implicit applies only to local variables.
What is the difference between an Interface and a Type Alias, and when would you choose one over the other?
Select the correct answer
Interfaces are faster at runtime; type aliases work only with plain object shapes here.
Interfaces support declaration merging; type aliases can express unions and primitives.
Interfaces exist at runtime; type aliases are completely erased during compilation steps.
Interfaces can express unions and tuples; type aliases support declaration merging fully.
What is the difference between union types and intersection types? Can you give an example of when you'd use each?
Select the correct answer
A union means a value is one of several types; an intersection means a value has all properties of every type combined.
A union restricts values to the shared members of the types; an intersection produces a type holding either listed type.
A union means a value has all properties of every type at once; an intersection means a value is just one of several types.
A union merges object types into a single combined shape; an intersection picks the common subset of properties they share.
What are tuple types in TypeScript, and how do they differ from regular arrays?
Select the correct answer
Tuples are objects with numeric keys and named fields, while regular arrays only support sequential numeric index access.
Tuples have a fixed length where each position holds a specific known type, unlike arrays of arbitrary length and one type.
Tuples are immutable arrays whose values cannot change after creation, while regular arrays allow elements added or removed.
Tuples have an arbitrary length where every position holds one shared type, unlike arrays which fix the type at each index.
What is the 'Non-null Assertion Operator' (!) and why is it generally discouraged in production code?
Select the correct answer
It asserts a value is not null or undefined, skipping checks; risky because it can mask genuine runtime null errors.
It forces a value to be null or undefined at runtime, adding checks; risky because it removes useful compile-time inference data.
It declares a value as optional in a function signature, adding checks; risky because it can break strict null checking entirely.
It converts a nullable value into a default fallback value, skipping checks; risky because it can silently swallow thrown exceptions.
What is the difference between optional chaining (?.) and the nullish coalescing operator (??), and when would you use each?
Select the correct answer
?. stops and yields undefined when accessing a property of a nullish value; ?? supplies a fallback only when the left value is nullish
?. throws an error when the operand is nullish at runtime; ?? silently swallows that error and substitutes the right-hand operand instead
Both return a default value, but ?. treats empty strings and zero as missing while ?? only checks against the undefined value alone
?. supplies a fallback value when the operand is nullish; ?? stops and yields undefined when a property access happens to fail
What does the strict flag in tsconfig.json actually do? Name at least three specific checks it enables such as strictNullChecks or noImplicitAny.
Select the correct answer
It forces every variable to carry an explicit type annotation and disallows the any type from appearing anywhere in the entire codebase
It only enables strictNullChecks and must be combined manually with noImplicitAny and strictFunctionTypes for coverage
It turns on a group of strict checks at once, including strictNullChecks, noImplicitAny, and strictFunctionTypes among others
It enables runtime validation of types and inserts checks into emitted JavaScript so null and undefined errors are caught while executing
What are Generics, and how do they improve code reusability without sacrificing type safety?
Select the correct answer
They convert types into runtime values that can be inspected, letting code branch on the actual type passed in during normal execution
They let a function accept any type at runtime by erasing all checks, so callers gain flexibility at the cost of full type safety
They duplicate a function for each concrete type at compile time, so reuse comes from generated overloads rather than a single definition
They let a function or type work over many types while preserving relationships between inputs and outputs, avoiding the loose any type
How does TypeScript help in handling null and undefined? Explain the Strict Null Checks feature and how it changes the way you write code.
Select the correct answer
null and undefined become assignable to any type, letting the compiler infer safe defaults automatically.
null and undefined are automatically narrowed away everywhere, so explicit checks become entirely unnecessary.
null and undefined throw compile errors on declaration, so variables must always be initialized immediately.
null and undefined are excluded from every type unless explicitly added, forcing you to handle them.