admin管理员组文章数量:1287484
I receive en error in typescript:
message: 'Type 'TransitionStyles' cannot be used as an index type.'
I would like to know if it is possible to change my interface so it can also be used and an index type
:
export interface TransitionStyles {
entering: Object
entered: Object
exiting: Object
exited: Object
[key: string]: Object
}
or I am forced to use a different interface as:
export type TransitionState = 'entering' | 'entered' | 'exiting' | 'exited'
I receive en error in typescript:
message: 'Type 'TransitionStyles' cannot be used as an index type.'
I would like to know if it is possible to change my interface so it can also be used and an index type
:
export interface TransitionStyles {
entering: Object
entered: Object
exiting: Object
exited: Object
[key: string]: Object
}
or I am forced to use a different interface as:
export type TransitionState = 'entering' | 'entered' | 'exiting' | 'exited'
-
Use
keyof
. Also, don't use theObject
type. Useobject
or{}
, depending on which you mean. – Aluan Haddad Commented Nov 23, 2017 at 13:18 - I have tries [key: TransitionState ] but does not work, could you please provide me with an example – GibboK Commented Nov 23, 2017 at 13:30
-
You have to use
type T = {[P in TransitionState]}
. It does not currently work for interfaces. – Aluan Haddad Commented Nov 23, 2017 at 13:48 - Could you write a full example? – GibboK Commented Nov 24, 2017 at 7:33
1 Answer
Reset to default 10I think you want something akin to
export interface TransitionStyles {
entering: object;
entered: object;
exiting: object;
exited: object;
}
export type TransitionState = keyof TransitionStyles;
export type PromisifiedTransitionStyles = {
[P in TransitionState]: Promise<TransitionStyles[P]>
};
Note that if we reintroduce the string
index signature, [key: string]: TransitionState
, the key type TransitionState
, will collapse to just string
because in the type string | "literal"
the more general type subsumes the more specific constituents of the union.
本文标签: javascriptType 39X39 cannot be used as an index typeStack Overflow
版权声明:本文标题:javascript - Type 'X' cannot be used as an index type - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741275379a2369696.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论