let Obj = function Obj<T>(gen: T) {
return {
data: <{[Key in keyof T]: T[Key]}>{},// need generic, result T[Key]
gen,
}
}({
foo(min: number, max: number, container: { data: any }) {
let res = ((max - min) / 2)
container.data.foo = res
return res//number
}
});
Obj.gen.foo(20,50,Obj)
let aaaaaaa: number = Obj.data.foo;//Obj.data.foo typeof function. must be number
Obj.data.foo
具有类型的功能。但必须是数字
请查看screen shot - 突出显示
我需要通用T [Key]的结果
。
。
。
type qqw<R> = () => R
;
function ZcddF<Q>(foo: qqw<Q>): Q {
return ;
}
function foo(): number { return ; }
let aaaa = ZcddF(foo)// 4a is number
;
type a = <Z>(foo: qqw<Z>) => Z
type b = () => number
;
let ff:a;
let bbbb = ff(foo)// 4b is number too
;
// type cccc = a(b) // <------------ QN how get this type/generic
答案 0 :(得分:0)
let fooN = () => 78
let fooS = () => ""
let fooArr = (): [string, number] => ["", 78]
interface iGen<T, R, K extends keyof T> {
[key: string]: () => R
}
function Obj<T, R, K extends keyof T, S extends {}>(gen: T & iGen<T, R, K>, Ssw?: S) {
let res: S & {
data: {[K in keyof T]: R}
gen: T
} = <any>Object.assign(Ssw, {
data: {},
gen,
})
return res
}
let obj = (
Obj({ fooS },
Obj({ fooN },
Obj({ fooArr },
)))
/*obj*/);
let gS = obj.gen.fooS //() => string
let S = obj.data.fooS //string
let gN = obj.gen.fooN //() => number
let N = obj.data.fooN //number
let gArr = obj.gen.fooArr //() => [string, number]
let Arr = obj.data.fooArr //[string, number]
let Arr0 = obj.data.fooArr[0] //string
let Arr1 = obj.data.fooArr[1] //number
等待发布并查看提案:获取typeof #6606和6606#issuecomment-178362393
的任何表达式的类型