# Function Variance

> Method syntax vs property syntax strictness.

Subject: TypeScript | Lesson 21 of 54 | 5 min read
Interactive lesson: https://codewithanimation.in/typescript/function-variance
Previous: [Typing Promises & Async](https://codewithanimation.in/typescript/typing-promises-async.md) | Next: [The this Parameter](https://codewithanimation.in/typescript/this-parameter.md)

## Bivariance vs Contravariance

This is one of the most hidden quirks in TypeScript. When you define a function on an interface, you can write it two ways:

1. Method syntax: `update(data: string): void;`

2. Property syntax: `update: (data: string) => void;`

They look identical, but TypeScript treats them completely differently when it comes to type checking subclasses (variance).

## The Strictness Gap

Method syntax is **bivariant**. It is historically loose and allows unsafe assignments (e.g. assigning a `Dog` that only eats `Bones` to an `Animal` variable that is allowed to eat any `Food`).

Property syntax is **contravariant**. It is mathematically strict and will throw an error if you try to make an unsafe assignment.

## Syntax Comparison

```typescript
class Animal {}
class Dog extends Animal {}

// Method Syntax (Loose / Bivariant)
interface EventLocker {
  lock(target: Animal): void;
}

// Property Syntax (Strict / Contravariant)
interface StrictLocker {
  lock: (target: Animal) => void;
}

// A function that only knows how to lock Dogs, not all Animals.
const dogLocker = (target: Dog) => {};

// OK: TypeScript allows this unsafe assignment because of method syntax.
const locker1: EventLocker = { lock: dogLocker };

// Error: TypeScript blocks this unsafe assignment.
// const locker2: StrictLocker = { lock: dogLocker };
```

## Common mistake: Runtime crashes

If you use method syntax and pass a `Dog`-only function into an `Animal` slot, the caller might pass a `Cat` into it. Your function will crash at runtime because it expected a `Dog`.

The TypeScript compiler won't warn you unless you use the strict property syntax.

## Tip: Always use property syntax

If you want true type safety, always define functions on your interfaces using the arrow function property syntax: `name: (args) => returnType;`.

## Try it: Try it

Watch how the loose Method Syntax allows an unsafe function to slip through the type checker, while the strict Property Syntax blocks it at the gate.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/typescript/function-variance

## Check yourself

### Question 1

Which interface definition provides stricter type checking for function assignments?

- A. interface API { fetch(url: string): void }
- B. interface API { fetch: (url: string) => void }
- C. They are exactly the same.

Correct answer: B

Property syntax (with the arrow function) enforces strict contravariance, preventing unsafe parameter assignments.

- Why not A: This is method syntax (bivariant/loose).
- Why not C: They compile to the same JS, but TS type checks them entirely differently.

## Remember this

- Method syntax (`fn(): void`) is bivariant (loose).
- Property syntax (`fn: () => void`) is contravariant (strict).
- Property syntax prevents runtime crashes caused by unsafe function arguments.
- Always prefer property syntax for function definitions on interfaces.
