R
R
Roman Yakimchuk2019-04-14 07:45:14
typescript
Roman Yakimchuk, 2019-04-14 07:45:14

Why is it possible to override a function that requires a different signature as input?

The code:

let foo: ({ x: number }) => void

foo = () => null
foo = function () {}

In theory, the function signature is not respected (argument "x" is not required in the assigned function), but there is no error.
Why is this code valid?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Robur, 2019-04-14
@Robur

firstly, your code is wrong, it should be visible because type checking is not just comparing function signatures - it is checking that one type can be cast to another. a function that does not have a parameter can be called in a place where a parameter is passed to it, it will simply ignore it. That is, the type of a function without parameters is converted to a type with parameters. In fact, if you call it, it will work fine, which the type system takes into account. try to do the opposite and immediately get an error: The same if the parameters do not match the types let foo: ( x: number ) => void

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question