Skip to content

Warning when writing a generator function without any yield #13847

Closed
@krryan

Description

@krryan

TypeScript Version: 2.2.0-dev.20170126

Code

export function* keyed<K, V>(values: Iterable<V>, toKey: (value: V) => K): IterableIterator<[K, V]> {
    for (const value of values) {
        return [toKey(value), value];
    }
}

Expected behavior:
Some kind of notice that this generator function never actually yields anything.

Actual behavior:
Compiles, and runs for that matter, without error, just silently yielding nothing.

I just spent a couple of hours tracking down a bug here, where return was accidentally used instead of yield. I'll admit I'm only starting to learn about generator functions and their uses, so it may be entirely possible that there are generator functions that want to return, rather than yield, in some cases, but my understanding is that every generator function should yield at some point.

Could we get a compiler warning for something like this? I'm not looking for a halt detector or anything :P just some kind of warning like "No 'yield' in reachable code," or something.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions