Typescript 创建映射类型以提示返回值

Typescript 创建映射类型以提示返回值,typescript,generics,Typescript,Generics,假设我正在处理与以下接口对应的对象: interface Foo { getCount(): number; doSomething(): boolean; } 它上面只有函数,没有一个函数是异步的。但是,我并不总是能够同步访问对象,在某些情况下,我将处理异步版本,其中所有函数返回值都封装在承诺中。像这样: interface AsyncFoo { getCount(): Promise<number>; doSomething(): Promise

假设我正在处理与以下接口对应的对象:

interface Foo {
    getCount(): number;
    doSomething(): boolean;
}
它上面只有函数,没有一个函数是异步的。但是,我并不总是能够同步访问对象,在某些情况下,我将处理异步版本,其中所有函数返回值都封装在承诺中。像这样:

interface AsyncFoo {
    getCount(): Promise<number>;
    doSomething(): Promise<boolean>;
}
但是
Self
Promise
都要求在我使用泛型时静态地给出它们,而不是以这种向后的方式使用它们

因此,接下来我尝试创建某种映射类型,例如:

type Promisify<T> = {[K in keyof T]: Promise<T[K]>}
但我似乎不能把所有的东西都放在一起


所以现在我在这里。我有没有办法将表示这种“Promisify”转换的类型(映射或其他方式)构建到类型的返回值上?

解决这一问题的一种方法是创建一个通用版本的
Foo
,它可以专门化为同步版本或异步版本

type MaybePromise<T, B extends 'plain' | 'promise'> = { 
  plain: T, 
  promise: Promise<T> 
}[B]
最后将其专门化:

interface Foo extends GenericFoo<'plain'> { }
interface AsyncFoo extends GenericFoo<'promise'> { }
希望有帮助。祝你好运

使用Typescript 2.8中的新版本,您可以执行以下操作:

// Generic Function definition
type AnyFunction = (...args: any[]) => any;
// Extracts the type if wrapped by a Promise
type Unpacked<T> = T extends Promise<infer U> ? U : T;

type PromisifiedFunction<T extends AnyFunction> =
    T extends () => infer U ? () => Promise<Unpacked<U>> :
    T extends (a1: infer A1) => infer U ? (a1: A1) => Promise<Unpacked<U>> :
    T extends (a1: infer A1, a2: infer A2) => infer U ? (a1: A1, a2: A2) => Promise<Unpacked<U>> :
    T extends (a1: infer A1, a2: infer A2, a3: infer A3) => infer U ? (a1: A1, a2: A2, a3: A3) => Promise<Unpacked<U>> :
    // ...
    T extends (...args: any[]) => infer U ? (...args: any[]) => Promise<Unpacked<U>> : T;

type Promisified<T> = {
    [K in keyof T]: T[K] extends AnyFunction ? PromisifiedFunction<T[K]> : never
}
//通用函数定义
键入AnyFunction=(…参数:any[])=>any;
//如果由承诺包装,则提取类型
类型unpacket=T扩展承诺?U:T;
类型PromisifiedFunction=
T扩展()=>推断U?()=>承诺:
T扩展(a1:推断a1)=>推断U?(a1:a1)=>承诺:
T扩展(a1:推断a1,a2:推断a2)=>推断U?(a1:a1,a2:a2)=>承诺:
T扩展(a1:推断a1,a2:推断a2,a3:推断a3)=>推断U?(a1:a1,a2:a2,a3:a3)=>承诺:
// ...
T扩展(…args:any[])=>推断U?(…args:any[])=>Promise:T;
指定的类型={
[K in keyof T]:T[K]扩展任何函数?PromisifiedFunction:从不
}
示例:

interface HelloService {
    /**
    * Greets the given name
    * @param name 
    */
    greet(name: string): string;
}

function createRemoteService<T>(): Promisified<T> { /*...*/ }

const hello = createRemoteService<HelloService>();
// typeof hello = Promisified<HelloService>
hello.greet("world").then(str => { /*...*/ })
// typeof hello.greet = (a1: string) => Promise<string>
接口HelloService{
/**
*欢迎你的名字
*@param name
*/
问候语(名称:string):string;
}
函数createRemoteService():Promisified{/*…*/}
const hello=createRemoteService();
//hello的类型=承诺
你好。问候(“世界”)。然后(str=>{/*…*/})
//typeof hello.greet=(a1:string)=>Promise

这不是我希望能够做到的,但它很有效,所以我会接受。谢谢
type GenericFoo<B extends 'plain' | 'promise'> = {
  getCount(): MaybePromise<number, B>;
  doSomething(): MaybePromise<boolean, B>;
}
interface Foo extends GenericFoo<'plain'> { }
interface AsyncFoo extends GenericFoo<'promise'> { }
declare const f: Foo;
if (f.doSomething()) {
  console.log(2 + f.getCount());
}

declare const aF: AsyncFoo; 
aF.doSomething().then(b => {
  if (b) {
    aF.getCount().then(n => {
      console.log(2 + n);
    })
  }
});
// Generic Function definition
type AnyFunction = (...args: any[]) => any;
// Extracts the type if wrapped by a Promise
type Unpacked<T> = T extends Promise<infer U> ? U : T;

type PromisifiedFunction<T extends AnyFunction> =
    T extends () => infer U ? () => Promise<Unpacked<U>> :
    T extends (a1: infer A1) => infer U ? (a1: A1) => Promise<Unpacked<U>> :
    T extends (a1: infer A1, a2: infer A2) => infer U ? (a1: A1, a2: A2) => Promise<Unpacked<U>> :
    T extends (a1: infer A1, a2: infer A2, a3: infer A3) => infer U ? (a1: A1, a2: A2, a3: A3) => Promise<Unpacked<U>> :
    // ...
    T extends (...args: any[]) => infer U ? (...args: any[]) => Promise<Unpacked<U>> : T;

type Promisified<T> = {
    [K in keyof T]: T[K] extends AnyFunction ? PromisifiedFunction<T[K]> : never
}
interface HelloService {
    /**
    * Greets the given name
    * @param name 
    */
    greet(name: string): string;
}

function createRemoteService<T>(): Promisified<T> { /*...*/ }

const hello = createRemoteService<HelloService>();
// typeof hello = Promisified<HelloService>
hello.greet("world").then(str => { /*...*/ })
// typeof hello.greet = (a1: string) => Promise<string>