I have this code:
const dataId = await getData(id);
const result = dataId.requestPayload.officePaymentId; // error here
I get Object is of type 'unknown'
The function getData()
is this one:
import { Agent } from '../types';
export default async function getData<T>(
id: string
): Promise<Agent<T>> {
const Item = [something];
return Item as Agent<T>;
}
the Agent
interface is:
export interface Agent<T> {
id: string;
requestPayload: T;
}
How can I pass the definiton of the generic when I call the function getData
? Or why I'm getting the mentioned error. Any hint please
You should pass the type to getData
const dataId = await getData<MyPayload>(id);
where MyPayload
is the type with the property officePaymentId
Typescript is waiting you to specify the type T
when calling getData
.
To do so use the <TYPE>func(arguments)
syntax, example:
const dataId = await getData<{
officePaymentId: string;
}>('foo');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.