Skip to content

Can't use destructuring parameter with inferred generic type #4937

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

Closed
Pajn opened this issue Sep 23, 2015 · 2 comments
Closed

Can't use destructuring parameter with inferred generic type #4937

Pajn opened this issue Sep 23, 2015 · 2 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@Pajn
Copy link

Pajn commented Sep 23, 2015

class GenericClass<T> {
  payload: T;
}

var genericObject = new GenericClass<{greeting: string}>();

function genericFunction<T>(object: GenericClass<T>, callback: (payload: T) => void) {
  callback(object.payload);
}

// Works
genericFunction(genericObject, (payload) => {
  // Here TS understands that payload have a property greeting
  // and that payload.greeting is a string.
});

// Fails to compile with "Type 'T' has no property 'greeting' and no index signature.".
// Type of greeting is any
genericFunction(genericObject, ({greeting}) => {

});
@joaomoreno
Copy link
Member

I don't think this is related to generics:

function f(fn: (o: { a: number })=>void) {}
f(({ a }) => { a /* is any, should be number */ })

@ahejlsberg
Copy link
Member

This particular issue is indeed related to destructuring of a generic type, but #4949 isn't.

@ahejlsberg ahejlsberg added the Fixed A PR has been merged for this issue label Sep 24, 2015
@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
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