3

Why there aren't any compilation errors in this code? Code:

function partial(f: (a: string) => string, a: string) : string {
  return "";
}

var test = () => "";
var result = partial(test, "");

Function "partial" takes as the first argument a function, that takes one parameter, but I pass to it function, that doesn't take any parameters, and typescript compiler thinks, that this is OK. I understand that this can't break anything because you can pass all parameters in the world to the function that doesn't take any, and it won't break anything, but it should be a compilation error because typescript is about types and there is an obvious type missmatch and it can possible be a developer's error.

Is there any workarounds for this problem?

1
  • 1
    maybe i'm missing a terminology step or something (i'm not a typescript user), but wouldn't a function with params and a function without still both be functions? why would it throw a type error? they are both the same type. Commented Sep 25, 2015 at 20:45

1 Answer 1

3

there is an obvious type missmatch and it can possible be a developer's error.

There's nothing obviously wrong with this code. Consider something like this:

let items = [1, 2, 3];
// Print each item in the array
items.forEach(item => console.log(item));

Is this code correct? Definitely! But forEach invokes its provided function with three arguments, not one. It would be tedious to have to write:

items.forEach((item, unused1, unused2) => console.log(item));

Note that you can still get errors if you try to do something that's actually wrong. For example:

function printNumber(x: number) { console.log(x); }
let strings = ['hello', 'world'];
strings.forEach(printNumber); // Error, can't convert string to number
Sign up to request clarification or add additional context in comments.

4 Comments

Well, I disagree with that. If you have a function that may take 1 parameter and some overloads that can take 2 or 3 parameters, you should create typings for all of that prototypes. There is no difference in my view. For your example, if forEach method on Array can take as first arg a function, that takes one arg, two args or three args, you should create 3 prototype for it in typings to show these three possibilities.
That doesn't model the runtime behavior of any function. forEach always invokes its argument with three parameters; it doesn't check the length of the callback to decide how many arguments to invoke it with.
It is quite reasonable to want the type system to catch errors like this. While forEach callbacks may not use all runtime arguments, most user-defined functions expect all parameters to be provided. They are even explicitly not marked as optional. The question is, is there a way to enforce this in Typescript.
There isn't, and there also isn't a good reason to enforce this. A program with an additional unused parameter isn't plausibly more correct than one without it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.