admin管理员组

文章数量:1302520

If I have the following basic function typescript can infer the return type automatically.

function myFunction(x: number, y: number) {
  return x * y;
}

Is it only useful to declare return types if typescript cannot infer the return type because some other call is leaking any and so it cannot make a proper inference?

function myFunction(x: number, y: number) {
  return x * y || callThatReturnsAny();
}

In this case I would want to type it if I know callThatReturnsAny() returns a number

function myFunction(x: number, y: number): number {
  return x * y || callThatReturnsAny();
}

Although the best solution would just be to type callThatReturnsAny() so that typescript can make the inference? But in that case when should you really ever use explicit return types?

If I have the following basic function typescript can infer the return type automatically.

function myFunction(x: number, y: number) {
  return x * y;
}

Is it only useful to declare return types if typescript cannot infer the return type because some other call is leaking any and so it cannot make a proper inference?

function myFunction(x: number, y: number) {
  return x * y || callThatReturnsAny();
}

In this case I would want to type it if I know callThatReturnsAny() returns a number

function myFunction(x: number, y: number): number {
  return x * y || callThatReturnsAny();
}

Although the best solution would just be to type callThatReturnsAny() so that typescript can make the inference? But in that case when should you really ever use explicit return types?

Share edited Dec 7, 2018 at 0:34 Adam Thompson asked Dec 7, 2018 at 0:27 Adam ThompsonAdam Thompson 3,5064 gold badges28 silver badges46 bronze badges 7
  • 1 Did you mean to use binary or? – Jared Smith Commented Dec 7, 2018 at 0:31
  • @JaredSmith yes, thank you. – Adam Thompson Commented Dec 7, 2018 at 0:34
  • It is never necessary, you can declare it whenever you like. – zerkms Commented Dec 7, 2018 at 0:38
  • @zerkms just to be more explicit as a style decision? Otherwise, I know it is not necessary - but does is there ever a good use case for doing so? – Adam Thompson Commented Dec 7, 2018 at 0:42
  • 1 if you use --noImplicitAny piler option it will give an error each time you use explicit any like in callThatReturnsAny; than you can type function with any type have the safe code and save some typing on the function where the piler can infer types – setdvd Commented Dec 7, 2018 at 3:34
 |  Show 2 more ments

1 Answer 1

Reset to default 11

I switch on noImplicitAny and avoid adding type annotations in almost all cases, except functions. Why? Because I don't want to accidentally return a union type when:

  1. I forget to return a value
  2. I return a value of the wrong type

For example, my day goes differently if I start off with:

function example(a: number, b: number) {

vs

function example(a: number, b: number): number {

Here's what happens next...

function example(a: number, b: number) {
    if (a > 5) {
        return 5;
    }

    if (b > a) {
        return 'b';
    }
}

My return type is now number | string | undefined.

If I use the return type annotation, I get additional help*.

It helps you return the correct type:

In strict mode, it makes sure you return something every time.

* if you like additional help, you'll also have all the strict things switched on.

本文标签: javascriptWhen is it necessary to declare return function type in TypeScriptStack Overflow