V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
Lxxyx
V2EX  ›  问与答

这种是什么 TypeScript 语法? this!.state!.imageSizes![index]

  •  
  •   Lxxyx · Jan 24, 2018 · 2441 views
    This topic created in 3027 days ago, the information mentioned may be changed or developed.

    RT.

    在 Github 的开源项目中看到这种写法,感觉有点诧异。

    一开始以为是 optional chaining operator,但是发现编译后的代码并没有 optional chaining 的特性

    项目用的是 TypeScript.

    代码出处: https://github.com/ascoders/react-native-image-viewer/blob/master/src/image-viewer.component.tsx#L126

    3 replies    2018-01-24 18:49:03 +08:00
    Kilerd
        1
    Kilerd  
       Jan 24, 2018
    optional,可以是有内容的,也可能是空的。
    所以在使用前先判断是否为空。
    让你确定他已经是有内容的情况下,可以在变量名后面!来强行解构。
    Ruiming
        3
    Ruiming  
       Jan 24, 2018 via iPhone
    非空断言,排除了前面空类型的情况,不然他就会提示你前面那个可能为空。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3461 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 49ms · UTC 10:41 · PVG 18:41 · LAX 03:41 · JFK 06:41
    ♥ Do have faith in what you're doing.