Maciej Kravchyk
Maciej Kravchyk

Reputation: 16647

TypeScript - void function passes as a a plain object, why?

Why does a void function pass as a plain object? Is there a good reason for this, or is it a bug?

playground

type PlainObject = { [key: string]: any };

const foo = (value: PlainObject) => { }

const voidFn: () => void = () => { };

// Error as expected
foo(false);
foo(3);
foo('test');

foo ({ test: true }) // ok - expected usage

foo(voidFn); // Why does it not trigger an error?

The problem occurs only if the value is any. If it was { [key: string]: string }; for instance, the error would occur.

Upvotes: 0

Views: 131

Answers (1)

Quentin
Quentin

Reputation: 943695

Functions are objects.

const f = () => null;
const o = {};

console.log(f instanceof Object);
console.log(o instanceof Object);

Since every property of the PlainObject can have any value, it doesn't matter what properties the function has.

Upvotes: 2

Related Questions