question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

noImplicitReturnType [Suggestion]

See original GitHub issue

We have noImplicitReturns which is great and helps tick up silly mistakes but it woud great to have noImplicitReturnType as well.

So what would noImplicitReturnType do? Currently we can write a function like so:

function life () {
    return 42;
}
let x = life(); // Number

So all is great, TypeScript works out the return type nicely for us.

function life (x: boolean) {
    if (x) {
        return 'hi';
    } else {
        return 42;
    }
}
let y = life(true); // Number / String

This is where things start to go a little crazy. TypeScript records y as of type number | string which is of course acurate. But what if i wanted this function to only return strings?

We already have support for that with limited modifications

function life (x: boolean): string {
    if (x) {
        return 'hi';
    } else {
        return 42; // Type '42' is not assignable to type 'string'.
    }
}
let y = life(true);

And as a result TypeScript nicly throws an error.

So my suggestion is that in much the same way as with noImplicitAny and let enforcing strict typeing an argument is added in the form of noImplicitReturnType which for my first example (only a single return) wouldn’t have any effect but for the second would throw an error instead of typing the function as number | string

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:6
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
aluanhaddadcommented, May 10, 2017

I worry that new developers will enable this feature long before they fully understand the nature and expressive power of TypeScript’s type system.

More concretely I think that adopting this would very likely result in

  1. Increased use of any
  2. Failure to understand and take advantage of union and intersection types
  3. Reduced benefits of CFA
  4. Reduced use of generics
  5. General confusion

There are already many misconceptions about TypeScript which have lead to poor coding practices that result in a failure to take advantage of the language.

Here is an unfortunately typical example from a Stack Overflow question:

export class ListComponent {
  items: any[] = [{id: 1, value: 'item1'}, {id: 2, value: 'item2'}, {id: 3, value: 'item3'}];
  myModel: any = {id: this.items[1].id , value: this.items[1].value};
}

We can all agree that such code is terrible, but why was it written that way?

I really want to stress that the above pattern is common.

I think adding this feature would be a serious mistake.

2reactions
andy-mscommented, May 10, 2017

You might be interested in the typedef tslint rule.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Suggestion: disallow implicit return of undefined #2316 - GitHub
It would be useful to be able to disallow an Explicitly Typed function/method from implicitly returning the value undefined.
Read more >
Why is there an "Implicit any return type" error here?
1 Answer 1 · The compiler needs to infer the return type of MyClass<T>. · To do this, it needs to know what...
Read more >
Opinion on explicit return types? : r/typescript - Reddit
28 votes, 27 comments. Hi All, What is your opinion on always mentioning return types on each of your method? I mean for...
Read more >
explicit-function-return-type | typescript-eslint
Require explicit return types on functions and class methods. ... Should indicate that no value is returned (void) function test() { return; }...
Read more >
Kinds of types - mypy 0.991 documentation
You should give a statically typed function an explicit None return type even if it doesn't return a value, as this lets mypy...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found