根据打字稿中的通用类型参数检查默认参数类型

时间:2019-11-26 14:30:53

标签: typescript typescript-generics

const defaultArg = (arg: {}) => ({})

type Arg<T> = (arg: T) => T;
type Default = Arg<{}>;

interface Test {
  (): void;
  <T>(arg: Arg<T>): void;
}

const test: Test = <T>(arg: Arg<T> = defaultArg) => { 
  //do something
}

TSError:

Type '(arg: {}) => {}' is not assignable to type 'Arg<T>'. Type '{}' is not assignable to type 'T'. '{}' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.

我想根据Arg的值确定传递给类型arg的通用参数。如果未定义arg,我想将{}传递为类型Arg而不是T的通用参数。

0 个答案:

没有答案