In Typescript, what is the ! (exclamation mark / bang) operator when dereferencing a member?

前端 未结 3 652
滥情空心
滥情空心 2020-11-22 00:54

When looking at the sourcecode for a tslint rule, I came across the following statement:

if (node.parent!.kind === ts.SyntaxKind.ObjectLiteralExpression) {
          


        
3条回答
  •  攒了一身酷
    2020-11-22 01:06

    Louis' answer is great, but I thought I would try to sum it up succinctly:

    The bang operator tells the compiler to temporarily relax the "not null" constraint that it might otherwise demand. It says to the compiler: "As the developer, I know better than you that this variable cannot be null right now".

提交回复
热议问题