Joe B
Joe B

Reputation: 1271

Using typescript how to define a type that returns same object with a promise for each value

I want to use a type (or interface) like this:

type FoodTable = {
  foodId: string;
  foodName: string;
};

And a function that accepts the type as a generic argument:

function promisifyData<T>(data): SomePromiseyData<T> {
  // code here
}

so if I invoke the function using FoodTable as the generic:

promisifyData<FoodTable>(someParam);

// returns:
{
  foodId: Promise {}, // resolves to string
  foodName: 'some name'
}

Basically I want to know how to define the SomePromiseyData type.

Here is a link to the typing I'm trying to achieve: typescriptlang.org/play

Upvotes: 0

Views: 27

Answers (2)

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174309

Your data parameter has no type. It should be of type T:

function makePromiseyData<T>(data: T) { /*...*/ }

See fixed version.

Upvotes: 4

SLaks
SLaks

Reputation: 887453

You need to declare your data parameter as having a type: data: T.

Upvotes: 1

Related Questions