Is there a way to achieve the following function in TypeScript?
function f<T extends 'a' | 'b'>(t : T): {[t]: ...} {
return {[t]: ...}
}
This code is intended to make f('a')
have type {'a': ...}
and similarly for 'b'. However, TypeScript throws errors when trying to implement this. I've searched through resources like Stack Overflow, Stack Overflow post on object interfaces with variable keys, and the official documentation on mapped types, but haven't found a clear solution.
Can someone provide insights on how to make this work in TypeScript?