代码之家  ›  专栏  ›  技术社区  ›  temporary_user_name

为什么在这个typescript react组件中的预期属性的对象结构中有分号?

  •  0
  • temporary_user_name  · 技术社区  · 6 年前

    import * as React from 'react';
    
    const Count: React.FunctionComponent<{
      count: number; // this is the line that confuses me
    }> = (props) => {
      return <h1>{props.count}</h1>;
    };
    
    export default Count;
    

    这个对象结构定义了预期的属性,但从什么时候开始,对象中的k-v对后面有分号在语法上是正确的呢?这一定是一个我不熟悉的Typescript语法,但我不确定它叫什么。

    0 回复  |  直到 6 年前
        1
  •  3
  •   Matt Tester    6 年前

    这相当于定义一个接口,只是没有名称,所以遵循语法也是如此 shown here :

    interface CountInterface {
        count: number;
    }
    

    它实际上不是一个键值对,而是一个键类型对。由于typescript需要的工作方式,这意味着它是一组语句(以分号结尾),尽管它看起来像一个对象文本,但它不是。

    所以,是的,这是一个“打字脚本”,允许进行类型检查。