Decorators & Project Setup

Preview — 3 of 10 questions

What does this strict flag add?

javascript
// tsconfig: "noUncheckedIndexedAccess": true

const arr: string[] = ["a", "b", "c"];
const first = arr[0]; // Type of first?

const obj: Record<string, number> = { a: 1 };
const val = obj["b"]; // Type of val?
Afirst: string, val: number — same as without the flag
BCompile error — indexed access is disallowed with this flag
Cfirst: string, val: number | undefined — only object access adds undefined
Dfirst: string | undefined, val: number | undefined — index access always adds | undefined

What's different with exactOptionalPropertyTypes?

javascript
// tsconfig: "exactOptionalPropertyTypes": true

interface Config {
  theme?: 'light' | 'dark';
}

const c1: Config = { theme: 'light' };   // (A) ?
const c2: Config = { theme: undefined }; // (B) ?
const c3: Config = {};                    // (C) ?
A(A) and (C) are valid; (B) errors — undefined is not the same as absent
BAll three are valid
C(A) is valid; (B) and (C) error
DAll three error — optional properties need | undefined in the type

What error does this flag catch?

javascript
// tsconfig: "noImplicitReturns": true

function getLabel(status: 'active' | 'inactive' | 'pending'): string {
  if (status === 'active') return 'Active';
  if (status === 'inactive') return 'Inactive';
  // 'pending' case missing!
}
AError — not all code paths return a value (function lacks a final return)
BNo error — TypeScript infers the function can return undefined
CError — the return type should be string | undefined
DError only if the missing case is reachable based on types

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.