typescript - 为什么在 Typescript 泛型中使用 '&'
问题描述
我是打字稿的新手。在下面的代码中,我看到使用带有“&”的泛型。我不明白这是什么意思?我认为 2 个接口可以加入并喜欢 1 个接口工作?
import * as React from 'react';
export interface StatefulCounterWithDefaultProps {
label: string;
initialCount?: number;
}
interface DefaultProps {
readonly initialCount: number;
}
interface State {
readonly count: number;
}
export const StatefulCounterWithDefault:
React.ComponentClass<StatefulCounterWithDefaultProps> =
class extends React.Component<StatefulCounterWithDefaultProps &
DefaultProps> {
static defaultProps: DefaultProps = {
initialCount: 0,
};
readonly state: State = {
count: this.props.initialCount,
};
componentWillReceiveProps({ initialCount }: StatefulCounterWithDefaultProps) {
if (initialCount != null && initialCount !== this.props.initialCount) {
this.setState({ count: initialCount });
}
}
handleIncrement = () => {
this.setState({ count: this.state.count + 1 });
}
render() {
const { handleIncrement } = this;
const { label } = this.props;
const { count } = this.state;
return (
<div>
<span>{label}: {count} </span>
<button type="button" onClick={handleIncrement}>
{`Increment`}
</button>
</div>
);
}
};
在这段代码中:React.Component<StatefulCounterWithDefaultProps &
DefaultProps>
2 个接口用 & 连接在一起。它是如何工作的?
解决方案
推荐阅读
- python - 从 Nodejs 到 Python 的数据交换无法正常工作
- java - 如何添加 onItemclicklistener 方法来回收按钮
- r - 是否有任何函数可以查找值是否为 R 中的整数?
- ubuntu - Apache2 SSL“SSL_ERROR_RX_RECORD_TOO_LONG”
- pandas - Timestamp 属性天的替代方案(似乎已被弃用)?
- reactjs - 私人路线 - 没有“新”就无法调用
- python - python从列表中删除单引号和空格
- redis - 尝试调用字段“replicate_commands”(零值)
- java - 尝试在任何布局中添加笔触宽度时,MaterialButton 是不可见的
- ruby-on-rails - 需要实现#cache!如果你想使用 Cloudinary::CarrierWave::Storage 作为缓存存储