Javascript 有没有可能得到;“已解决”;什么样的承诺?

Javascript 有没有可能得到;“已解决”;什么样的承诺?,javascript,typescript,Javascript,Typescript,我有一个从库中定义的函数 export declare const getUser: (id: string) => Promise<User>; export declare const getUser:(id:string)=>Promise; 我试图得到承诺所解决的类型 我能够做到以下几点以获得承诺: const fetchUserSuccess = createAction( 'users/GET_ONE_SUCCESS', (user: ReturnTyp

我有一个从库中定义的函数

export declare const getUser: (id: string) => Promise<User>;
export declare const getUser:(id:string)=>Promise;
我试图得到承诺所解决的类型

我能够做到以下几点以获得承诺:

const fetchUserSuccess = createAction(
  'users/GET_ONE_SUCCESS',
  (user: ReturnType<typeof API.getUser>) => user,
);
const fetchUserSuccess=createAction(
“用户/获得一次成功”,
(用户:ReturnType)=>用户,
);
但是,当我需要提取
user


typescript可以这样做吗?

您可以使用条件类型:

export type UnBoxPromise<T> = T extends Promise<infer U> ? U : never;
export declare const getUser: (id: string) => Promise<User>;
const fetchUserSuccess = createAction(
    'users/GET_ONE_SUCCESS',
    (user: UnBoxPromise<ReturnType<typeof getUser>>) => user,
);
导出类型unexpromise=T扩展承诺?U:从来没有;
导出declare const getUser:(id:string)=>Promise;
const fetchUserSuccess=createAction(
“用户/获得一次成功”,
(用户:unexpromise)=>用户,
);

您可以使用条件类型:

export type UnBoxPromise<T> = T extends Promise<infer U> ? U : never;
export declare const getUser: (id: string) => Promise<User>;
const fetchUserSuccess = createAction(
    'users/GET_ONE_SUCCESS',
    (user: UnBoxPromise<ReturnType<typeof getUser>>) => user,
);
导出类型unexpromise=T扩展承诺?U:从来没有;
导出declare const getUser:(id:string)=>Promise;
const fetchUserSuccess=createAction(
“用户/获得一次成功”,
(用户:unexpromise)=>用户,
);