TypeScript如何支持这种类型的泛型?

时间:2019-05-07 12:05:15

标签: typescript

  

似乎此ts#1213已经存在github问题。

这种用法更像是高阶函数。 我不确定TS是否支持。

接口相同,但是要实现同步和异步版本。


interface IO<T> {
  get: () => T<number>;
  set: (v: number) => T<boolean>;
}

type Raw<T> = T;

class SyncO implements IO<Raw> {
  get() {
    return 1;
  }

  set(v) {
    v;
    return true;
  }
}

class AsyncO implements IO<Promise> {
  get() {
    return Promise.resolve(1);
  }

  set(v) {
    v;
    return Promise.resolve(true);
  }
}

2 个答案:

答案 0 :(得分:0)

不完全确定为什么您的T不是通用的,而是直接在界面中使用。那是语法错误。

但是,如果您想同时使用两者的合并接口,则可以将联合类型与|一起使用

type syncOrAsync<T> = T | Promise<T>; 

interface IO<T> {
  get: () => syncOrAsync<T>;
  set: (v: number) => syncOrAsync<boolean>;
}

class SyncO implements IO<syncOrAsync<number>> {
  get() {
    return 1;
  }

  set(v: number) {
    return true;
  }
}

class AsyncO implements IO<syncOrAsync<number>> {
  get() {
    return Promise.resolve(1);
  }

  set(v: number) {
    return Promise.resolve(true);
  }
}

答案 1 :(得分:0)

当我阅读RESTyped的代码时,只需找到答案即可

type MaybePromise<T, P extends boolean> =
  P extends true ? Promise<T> : T;

interface IO<P extends boolean> {
  get: () => MaybePromise<number, P>;
  set: (v: number) => MaybePromise<boolean, P>;

  invalidSet: (v: number) => MaybePromise<boolean, P>;
}

class SyncO implements IO<false> {
  get() { return 1; }
  set(v) { return true; }

/* ts error:
(method) SyncO.invalidSet(v: any): Promise<boolean>
Property 'invalidSet' in type 'SyncO' is not assignable to the same property in base type 'IO<false>'.
  Type '(v: any) => Promise<boolean>' is not assignable to type '(v: number) => boolean'.
    Type 'Promise<boolean>' is not assignable to type 'boolean'.ts(2416)
*/
  invalidSet(v) { return Promise.resolve(true); }
}

class AsyncO implements IO<true> {
  get() { return Promise.resolve(1); }
  set(v) { return Promise.resolve(true); }

/* ts error:
(method) AsyncO.invalidSet(v: any): boolean
Property 'invalidSet' in type 'AsyncO' is not assignable to the same property in base type 'IO<true>'.
  Type '(v: any) => boolean' is not assignable to type '(v: number) => Promise<boolean>'.
    Type 'boolean' is not assignable to type 'Promise<boolean>'.ts(2416)
*/
  invalidSet(v) { return true; }
}