我在打字稿中收到错误:
消息:'输入' TransitionStyles'不能用作索引类型。'
我想知道是否可以更改我的界面,以便它也可以使用index type
:
export interface TransitionStyles {
entering: Object
entered: Object
exiting: Object
exited: Object
[key: string]: Object
}
或者我被迫使用不同的界面:
export type TransitionState = 'entering' | 'entered' | 'exiting' | 'exited'
答案 0 :(得分:2)
我想你想要类似于
的东西<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.trial</groupId>
<artifactId>books</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<springframework.version>5.0.1.RELEASE</springframework.version>
</properties>
<dependencies>
<!-- SPRING -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${springframework.version}</version>
</dependency>
...
</dependencies>
</project>
请注意,如果我们重新引入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]>
};
索引签名,索引类型string
将折叠为TransitionState
。这是因为在string
类型中,更一般的类型包含了更具体的联合成分。