【发布时间】:2019-01-07 03:04:38
【问题描述】:
我必须遵循以下功能:
function x<V = string, K extends string = string>(myKey: K): {[k in K]: V} {
return null as any;
}
我想去掉(或省略)K extends string = string 部分。
目前我不得不这样称呼它:
const res = x<number, 'foo'>('foo'); // resulting type: { foo: number }
但我不想输入两次foo。我只是想这样使用它:
const res = x<number>('foo');
虽然理想情况下我想这样输入函数:
function x<V = string>(myKey: string): {[myKey]: V} {
return null as any;
}
这有可能吗?
【问题讨论】:
标签: typescript generics typescript-generics