Imagine a scenario where...
interface Group {
name: string
isPublic: boolean
}
interface User {
id: string
age: number
}
type EntityType = 'group' | 'user'
function doTask(type: EntityType, entity: Group | User): boolean {
if (type === 'group') {
return entity.isPublic
}
if (type === 'user') {
return entity.age > 18
}
}
I am seeking a way to specify that when the first argument is group
, the entity
should conform to the Group
interface... It seems like Generics might be the solution, but I am struggling with the syntax.
How can I express "When type
is value X then entity
follows interface Y"?
Thanks in advance!
Edit: Changed to a Minimal Reproducible Example.