【问题标题】:Why use '&' in Typescript generic为什么在 Typescript 泛型中使用 '&'
【发布时间】:2019-03-08 09:50:27
【问题描述】:

我是 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 个接口用 & 连接在一起。 它是如何工作的?

【问题讨论】:

标签: typescript


【解决方案1】:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-02
  • 2020-04-11
  • 2019-04-02
  • 2019-05-20
  • 2017-08-04
相关资源
最近更新 更多