Skip to content

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

Description

@Pajn
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}) => {

});

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions