/**
* Declare the type for the following fuction
* e.g. const mappingFunc = obj => ({[obj.name]: true})
**/
const mappingFunc = <T extends string>(arg: {name: T}): Record<T, boolean> => ({
[arg.name]: true
})
const obj = {
name: 'foo' as 'foo' // <-- narrow down the type from `string` to `"foo"` (String Literal Type)
}
const output = mappingFunc<typeof obj.name>(obj);
console.log(output.foo) // true
console.log(output.bar) // error: TS2339: Property 'bar' does not exist on type 'Record<"foo", boolean>'.