【发布时间】:2019-05-30 07:47:24
【问题描述】:
我想根据泛型值返回不同的类型。例如:
interface Base {
key: string[];
}
class Test<T> {
value = [];
getSomething(): (T extends Base ? string[] : string) {
if (Array.isArray(this.value)) {
return ['a'] as string[];
}
return 'nothing' as string;
}
}
const v = new Test<Base>().getSomething();
但我收到一个错误:
类型 string[] 不能分配给类型 T extends Base ?细绳[] : 字符串
【问题讨论】:
标签: typescript