Below I have a function getEntityPropId
and it returns a object with one prop, like { 'thingId': 1 }
the string thing
is passed into the function.
I am curious as to how I can have the function return type include the key thingId
and not just any string, because I am passing in everything needed to know the return.
However I get this:
A computed property name in a type literal must refer to an expression whose type is a literal type or a 'unique symbol' type.ts(1170)
export const getEntityPropId = (value: any, entity: string): { [`${entity}Id`]: number } | null => {
const id = getEntityId(value, entity)
if (id !== null) return { [`${entity}Id`]: id }
return id
}
I'd be ok having to setup an enum
if necessary.
enum EntityPropIds {
thing = 'thingId',
}
export const getEntityPropId = (value: any, entity: EntityPropIds): { [EntityPropIds[entity]]: number } | null => {
const id = getEntityId(value, entity)
if (id !== null) return { [EntityPropIds[entity]]: id }
return id
}
Is this possible?