Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generator type inference error #14043

Closed
weswigham opened this issue Feb 13, 2017 · 1 comment
Closed

Generator type inference error #14043

weswigham opened this issue Feb 13, 2017 · 1 comment
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@weswigham
Copy link
Member

TypeScript Version: 2.1.6 and 2.2.0-dev.20170213

Code

tsconfig.json:

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es6",
        "noImplicitAny": true,
        "sourceMap": true
    }
}

test.ts:

export interface StrategicState {
    lastStrategyApplied?: string;
}

export function strategy<T extends StrategicState>(stratName: string, gen: (a: T) => IterableIterator<T | undefined>): (a: T) => IterableIterator<T | undefined> {
    return function*(state) {
        for (const next of gen(state)) {
            if (next) {
                next.lastStrategyApplied = stratName;
            }
            yield next;
        }
    }
}

export interface Strategy<T> {
    (a: T): IterableIterator<T | undefined>;
}

export interface State extends StrategicState {
    foo: number;
}

export const Nothing: Strategy<State> = strategy("Nothing", function*(state: State): IterableIterator<State | undefined> {
    return state;
});

Expected behavior:
Compiles without error.

Actual behavior:
On line 24, on the generator star: Generator implicitly has type 'IterableIterator<any>' because it does not yield any values. Consider supplying a return type.. This is unfixable without a terrible hack (add a line with the text if (!!false) yield state; to the top of the generator body), as there is already a return type specified. Actually, the return type (and argument type) should be inferred from the type annotation on the LHS of the equals sign and the strategy function signature (and the return type should not be required), but its not, though that's possibly a separate issue.

@mhegazy mhegazy added the Bug A bug in TypeScript label Feb 14, 2017
@mhegazy mhegazy added this to the TypeScript 2.3 milestone Feb 14, 2017
@mhegazy mhegazy assigned rbuckton and yuit and unassigned rbuckton Feb 14, 2017
@yuit
Copy link
Contributor

yuit commented Feb 22, 2017

hi @weswigham !!! 🐱 This turned out to be an issue from how we aggregate return type from function body. We have a check for generator that only aggregate return type from yield expression...

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

4 participants