Lesson overview · Free interview practice

Type Inference

Understand TypeScript type inference and compatibility.

Topics in the full lesson
  • Introduction to Type Inference
  • Type Inference in TypeScript
  • Structural Typing in TypeScript
  • Compatibility Between Types
  • Function Type Compatibility
  • Class and Interface Compatibility
  • Advanced Type Compatibility
  • Modern Inference Tools (TS 3.4+, TS 4.9+)
  • Best Practices
  • Exercises

Try the free interview questions

Work through a question before revealing its explanation. These questions and answers are free; Premium adds the full lesson walkthrough, examples and implementation detail.

TheoryEasy

How TypeScript Infers Types: Variables, Returns, and Widening

Question

How does TypeScript figure out types when you don't annotate them? Show inference for variables, function returns, and generic arguments.

Take a moment to think about this before revealing the answer

Explain your reasoning or try an implementation before comparing answers.

TheoryMedium

Contextual Typing: How TS Uses Surroundings to Infer

Question

What is contextual typing in TypeScript, and how does it differ from inferring a type from a value? Show three canonical examples.

Take a moment to think about this before revealing the answer

Explain your reasoning or try an implementation before comparing answers.

TheoryHard

Variance: Covariance, Contravariance, Bivariance

Question

What is variance in TypeScript? Distinguish covariance, contravariance, and bivariance with concrete examples.

Take a moment to think about this before revealing the answer

Explain your reasoning or try an implementation before comparing answers.

TheoryHard

Function Type Compatibility Rules

Question

When is a function type assignable to another? Show the rules for parameters, return types, and the practical surprise of "fewer params is more compatible."

Take a moment to think about this before revealing the answer

Explain your reasoning or try an implementation before comparing answers.

TheoryHard

Class Compatibility with Private and Protected Members

Question

TS's type system is structural — but there's one major exception. How do private and protected members affect class compatibility?

Take a moment to think about this before revealing the answer

Explain your reasoning or try an implementation before comparing answers.

Explore the full Type Inference material

Premium includes the complete lessons and implementation references. Free practice questions remain available without a subscription.

All course tracks & premium content
From basics to advanced masterclasses
Built for JS/TS developers like you
Real-world tips & common pitfalls
Upgrade to Premium

Continue in this section