3

I am new to Typescript and have a doubt regarding function type.

I have the following type :

type IMyTest<R> = (
    arg1: number,
    arg2: string,
    arg3: number
) => R;

Now, following signature is valid (doesn't give me an error) and I don't understand why its valid. As per my understandings, arg2 and arg3 are not marked as optional using the "?" sign and so the TypeScript compiler should raise an error, but it's not:

const myTest3: IMyTest<string> = (arg1: number):string => {

    return "any string";
};

Live example on the playground

Is there any way to make the arguments mandatory ?

Right now, arg1?:string is same as arg1:string. According to my understandings,without the "?" sign, an argument is mandatory. But, that's not the case in here.

2
  • You can call the function with 3 arguments and it would be fine, hence it's valid. You can always pass more arguments than necessary. Commented Jan 27, 2020 at 7:47
  • Ya. But, is there any way to make the arguments mandatory ? Right now, arg1?:string is same as arg1:string. According to my understandings,without the "?" sign, an argument is mandatory. But, that's not the case in here. Commented Jan 27, 2020 at 7:55

1 Answer 1

2

Have a look at type compatibility rules

let x = (a: number) => 0;
let y = (b: number, s: string) => 0;

y = x; // OK
x = y; // Error

"Discarding" parameters (like in your example) is allowed because the implementation doesn't care if these parameters provided or not (you won't get any error at runtime).

This pattern is actually quite common in JavaScript. For example, Array#forEach provides three parameters to the callback function: the array element, its index, and the containing array. Nevertheless, it’s very useful to provide a callback that only uses the first parameter.

const items = [1, 2, 3];

// Don't force these extra parameters
items.forEach((item, index, array) => console.log(item));

// Should be OK!
items.forEach(item => console.log(item));
Sign up to request clarification or add additional context in comments.

8 Comments

Btw, you mean y == x and x == y, right ? Is there anyway to make the arguments mandatory ? Thanks.
This explains why it works, but the question is how to require that the function have those parameters. Are you saying it's not possible? (That would surprise me, given how powerful TypeScript is.)
Btw, you mean y == x and x == y? No I'm explaining why assignment is allowed
@AlekseyL. Ya. You are right. My mistake. Thanks. I found a solution. Though I can write the function by omitting the arguments, but, I can't call it without providing 3 arguments. It does make sense now.
@AlekseyL. - Not offhand, no. :-) But I still want to know if it's possible.
|

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.