//fake code;there shoud be type annotations
function makeObject(key,value){
return {[key]:value};
}
const obj=makeObject("name","Tom");
what do I want
I want the typescript compiler to infer obj has type {"name":string}, but I have no idea what the signature of "makeObject" should be.
what I have tried
I have figure out how to make tsc recognize "name as a string literal type.
function makeKey<Key extends keyof {[k:string]:any}>(key:Key):Key{
return key;
}
const key=makeKey("name")//tsc infer "key" has type "name" not string
my question
is this possible with current typescirpt(3.9+)? if it is then how? Thanks in advance!