Vamshi
Vamshi

Reputation: 9331

Dynamic interfaces with generics

I am simplifying the question here .

interface Identity{
    name: string;
}

Another generic interface

interface State<T extends Identity>{
    [T.name] : StateContainer<T>
}

But this wont work as it gives error

Cannot find T

If I try to generate it by keeping it inside a function

function generate(c : Identity){
    interface State<T>{
        [c.name]: StateContainer<T>
    }
}

It says

A computed property name in an interface must directly refer to built-in symbol.

My desired output is to have dynamic interface such that . State<Tenant> should behave

interface State{
    'tenant': ....
}

Upvotes: 3

Views: 1546

Answers (1)

Saravana
Saravana

Reputation: 40554

You can use mapped types:

type State<T extends Identity> = {
    [P in keyof T] : StateContainer<T>
}

Upvotes: 3

Related Questions