strictFunctionTypes
strictFunctionTypes — why it matters
strictFunctionTypes checks function parameter compatibility more safely, especially when functions are stored in variables. A callback that only understands Dogs should not be treated as a callback that can receive any Animal. There are intentional compatibility exceptions for method syntax, so learn the rule in context.
What you’ll learn
- Explain strictfunctiontypes and the problem it solves.
- Identify the relevant compile-time checks and runtime responsibilities.
- Run the example, predict its output, and try a realistic modification.
Understand the mechanism
describeAnimal accepts any Animal, so it can safely handle a Dog. The reverse assignment can be unsafe because a generic Animal may lack bark.
Parameter variance is not simply interchangeable: accepting a broader input is different from requiring a narrower one.
Follow the concept flow
Run the primary TypeScript example
Save as strictfunctiontypes.ts. The following program passed strict TypeScript compilation, and the displayed output was captured by executing its emitted JavaScript with Node.js 22.16.0.
TypeScript source
type Animal = { name: string };
type Dog = Animal & { bark: () => void };
const describeAnimal: (a: Animal) => string = a => a.name;
const describeDog: (d: Dog) => string = describeAnimal;
const dog: Dog = { name: "Rex", bark: () => {} };
console.log(describeDog(dog));Verified output
RexCompile: npx tsc --strict --target es2022 --module commonjs strictfunctiontypes.ts; run: node strictfunctiontypes.js. For Node.js API examples, install @types/node first. For browser projects, use the browser-specific instructions below.
The example demonstrates the code-level behavior; the related configuration or test commands are shown separately when needed.
Trace the result
describeAnimal accepts any Animal, so it can safely handle a Dog. The reverse assignment can be unsafe because a generic Animal may lack bark.
Predict what changes if you alter one input. Then run the code to compare your reasoning with the real output.
Common mistake and correction
Parameter variance is not simply interchangeable: accepting a broader input is different from requiring a narrower one.
Identify whether the failure is a type-checking issue, a configuration issue, or a runtime behavior issue before choosing a fix.
Practice the concept
Try assigning a callback requiring Dog to a variable declared to accept Animal, and study the strictFunctionTypes error.
Write down your expected result first. For configuration examples, compare compiler output and diagnostics; for test examples, make the assertion fail deliberately to verify that the tool reports it.
Check your understanding
Official reference
For the exact current options and platform behavior, consult the official documentation. These examples were verified using the tool versions described above.
What to remember
strictFunctionTypes checks function parameter compatibility more safely, especially when functions are stored in variables.
Run the primary example, then complete the practice task or environment-specific lab.
Reaching the recap and completing the quizzes are separate milestones.