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

Infer type for generic. #5652

Closed
Strate opened this issue Nov 13, 2015 · 3 comments
Closed

Infer type for generic. #5652

Strate opened this issue Nov 13, 2015 · 3 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@Strate
Copy link

Strate commented Nov 13, 2015

Hi!

Seems that some cases of inferring types does not work:

interface Blabla<T> {
    (): T
}

function some(): Blabla {
    return () => {
        return 123;
    }
}

I got compilation error:

error TS2314: Generic type 'Blabla<T>' requires 1 type argument(s).

Is it possible to infer number type for T?

@ahejlsberg
Copy link
Member

No, but you can just omit the type annotation and TypeScript will infer the return type of some to be () => number. That type is structurally compatible with Blabla<number> so you get the same effect:

interface Blabla<T> {
    (): T
}

function some() {
    return () => {
        return 123;
    }
}

var f: Blabla<number> = some();

@ahejlsberg ahejlsberg added the Question An issue which isn't directly actionable in code label Nov 13, 2015
@Strate
Copy link
Author

Strate commented Nov 13, 2015

It would be very great if typescript infer type in this case.
In real life I have a bit more complex example:

interface Action {
  // some properties here
}
interface Dispatch {
    (action: Action): void
}
interface State {
  // some properties here
}
interface Thunk<R> {
    (dispatch: Dispatch, getState?: () => State): R
}

function doAction(): Thunk {
    return (dispatch, getState) => { // dispatch and getState arguments proper typed here
        return 123;
    }
}

without return type annotation I should annotate arguments for returning closure (I use ts with noImplicitAny compiler option). And now I should explicitly write Thunk<number>, but compiler really can infer that type.

@bedorlan
Copy link

bedorlan commented Jun 9, 2017

I had the exact same problem. I solved it like this:

interface Action {
  // some properties here
}
interface Dispatch {
    (action: Action): void
}
interface State {
  // some properties here
}
interface Thunk<R> {
    (dispatch: Dispatch, getState?: () => State): R
}

function doAction() {
    return createThunk((dispatch, getState) => {
        return 123;
    })
}

function createThunk<T>(f: Thunk<T>): Thunk<T> {
    return f
}

Have you found a better way?

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants