3

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': ....
}
4

1 回答 1

3

您可以使用映射类型

type State<T extends Identity> = {
    [P in keyof T] : StateContainer<T>
}
于 2017-08-07T04:11:00.383 回答