Bird
Raised Fist0
Typescriptprogramming~5 mins

Control flow analysis behavior in Typescript - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is control flow analysis in TypeScript?
Control flow analysis is TypeScript's way of understanding how your code runs step-by-step to check types and catch errors before running the program.
Click to reveal answer
beginner
How does TypeScript narrow types using control flow analysis?
TypeScript narrows types by checking conditions like if statements or type checks, so it knows more specific types inside those blocks.
Click to reveal answer
beginner
What happens when you use an 'if' statement with a type guard in TypeScript?
Inside the 'if' block, TypeScript treats the variable as the narrowed type, helping you avoid errors and write safer code.
Click to reveal answer
intermediate
Explain how TypeScript handles control flow with variables that can be null or undefined.
TypeScript tracks checks like 'if (variable != null)' to know when a variable is safe to use without null or undefined errors.
Click to reveal answer
intermediate
What is the effect of reassignment on control flow analysis in TypeScript?
When a variable is reassigned, TypeScript updates its understanding of the variable's type, which can widen or narrow the type depending on the new value.
Click to reveal answer
What does TypeScript do when it sees an 'if (typeof x === "string")' check?
AIt treats x as a number inside the if block.
BIt ignores the type check.
CIt treats x as a string inside the if block.
DIt throws a runtime error.
If a variable can be string or undefined, what does TypeScript infer after 'if (variable)' check?
Avariable is undefined inside the if block.
Bvariable is string inside the if block.
Cvariable is string or undefined inside the if block.
Dvariable is never inside the if block.
What happens if you reassign a variable inside a block in TypeScript?
AThe variable's type becomes any.
BTypeScript ignores the reassignment.
CTypeScript crashes.
DTypeScript updates the variable's type based on the new value.
Which of these helps TypeScript narrow types during control flow analysis?
AType guards like 'typeof' or 'instanceof'.
BComments in the code.
CVariable names.
DWhitespace formatting.
What does control flow analysis NOT do?
AChange the actual runtime behavior of code.
BPredict runtime errors by checking types.
CNarrow types inside conditional blocks.
DTrack variable assignments.
Describe how TypeScript uses control flow analysis to narrow variable types inside an if statement.
Think about how TypeScript understands conditions to know more about variable types.
You got /4 concepts.
    Explain the impact of variable reassignment on TypeScript's control flow analysis.
    Consider what happens when a variable gets a new value.
    You got /4 concepts.

      Practice

      (1/5)
      1.

      What does TypeScript's control flow analysis primarily help with?

      easy
      A. Running the program faster by optimizing loops
      B. Tracking variable types through code paths to catch errors early
      C. Automatically fixing syntax errors in the code
      D. Converting TypeScript code into JavaScript

      Solution

      1. Step 1: Understand control flow analysis purpose

        Control flow analysis tracks how variables change types through different code paths.
      2. Step 2: Identify its main benefit

        This helps TypeScript catch type errors before running the program.
      3. Final Answer:

        Tracking variable types through code paths to catch errors early -> Option B
      4. Quick Check:

        Control flow analysis = type tracking [OK]
      Hint: Control flow = tracking variable types in code paths [OK]
      Common Mistakes:
      • Confusing control flow analysis with code execution speed
      • Thinking it fixes syntax errors automatically
      • Believing it compiles TypeScript to JavaScript
      2.

      Which of the following TypeScript code snippets correctly narrows a variable's type using control flow analysis?

      let value: string | number = 5;
      if (typeof value === 'string') {
        console.log(value.toUpperCase());
      }
      easy
      A. The code should use instanceof instead of typeof
      B. The code will cause a runtime error because value can be a number
      C. The code needs a type assertion to call toUpperCase()
      D. The code is correct and safe to call toUpperCase() inside the if block

      Solution

      1. Step 1: Check the type guard condition

        The condition typeof value === 'string' narrows value to string inside the if block.
      2. Step 2: Confirm safe method call

        Since value is string here, calling toUpperCase() is safe and valid.
      3. Final Answer:

        The code is correct and safe to call toUpperCase() inside the if block -> Option D
      4. Quick Check:

        Type guard narrows type = safe method call [OK]
      Hint: Use typeof checks to narrow union types safely [OK]
      Common Mistakes:
      • Calling string methods without type narrowing
      • Confusing typeof with instanceof for primitives
      • Assuming type assertions are always needed
      3.

      What will be the output of this TypeScript code?

      function checkValue(x: string | number) {
        if (typeof x === 'number') {
          return x.toFixed(2);
        } else {
          return x.toUpperCase();
        }
      }
      console.log(checkValue('hello'));
      console.log(checkValue(3.14159));
      medium
      A. Runtime error because toFixed is not a function
      B. "hello" and "3.14159"
      C. "HELLO" and "3.14"
      D. "HELLO" and "3.14159"

      Solution

      1. Step 1: Analyze the first call with string

        For 'hello', the else block runs, returning 'HELLO' after toUpperCase().
      2. Step 2: Analyze the second call with number

        For 3.14159, the if block runs, returning '3.14' after toFixed(2).
      3. Final Answer:

        "HELLO" and "3.14" -> Option C
      4. Quick Check:

        Type guards produce correct string outputs [OK]
      Hint: Check typeof to predict which block runs [OK]
      Common Mistakes:
      • Assuming toFixed returns a number, not string
      • Ignoring type narrowing in if-else
      • Mixing up output formatting
      4.

      Identify the error in this TypeScript code related to control flow analysis:

      function process(input: string | null) {
        if (input) {
          console.log(input.toLowerCase());
        } else {
          console.log(input.toLowerCase());
        }
      }
      medium
      A. Calling toLowerCase() on null in the else block causes a runtime error
      B. The if condition should be input !== null instead of input
      C. TypeScript will automatically fix this error during compilation
      D. There is no error; the code is safe

      Solution

      1. Step 1: Check the else block usage

        In the else block, input is null, so calling toLowerCase() causes a runtime error.
      2. Step 2: Understand control flow limitation

        Control flow analysis narrows input in the if block but not in else, so unsafe call happens.
      3. Final Answer:

        Calling toLowerCase() on null in the else block causes a runtime error -> Option A
      4. Quick Check:

        Null check needed before method call [OK]
      Hint: Else block may have null; avoid method calls there [OK]
      Common Mistakes:
      • Assuming if(input) excludes null everywhere
      • Expecting TypeScript to fix runtime errors
      • Using loose null checks without safe calls
      5.

      Given this TypeScript code, what is the best way to fix the control flow analysis issue to avoid errors?

      function formatInput(input: string | number | null) {
        if (typeof input === 'string') {
          return input.trim();
        } else if (typeof input === 'number') {
          return input.toFixed(1);
        }
        return input.trim();
      }
      hard
      A. Add a null check before the last return to avoid calling trim() on null
      B. Remove the last return statement because it's unreachable
      C. Change all trim() calls to toString()
      D. Use a type assertion to force input as string in the last return

      Solution

      1. Step 1: Identify the problem with last return

        The last return input.trim() runs when input is null, causing an error.
      2. Step 2: Fix by adding null check

        Adding a check like if (input !== null) before calling trim() prevents errors.
      3. Final Answer:

        Add a null check before the last return to avoid calling trim() on null -> Option A
      4. Quick Check:

        Null checks prevent unsafe method calls [OK]
      Hint: Always check for null before calling methods on unions [OK]
      Common Mistakes:
      • Ignoring null possibility in union types
      • Assuming last return is unreachable
      • Using type assertions without safety
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard