xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

TypeScript !. & ?. operators All In One

TypeScript !. & ?. operators All In One

TypeScript Bang! (non-null assertion operator) !.

postfix operator / 非空断言操作符

const columns = [{width: 100}];
const widthColumnCount = columns!.filter(({ width }) => !width).length;

/* 

"use strict";
const columns = [{ width: 100 }];
const widthColumnCount = columns.filter(({ width }) => !width).length;

 */

?.

可选链操作符

const columns = [{width: 100}];
const widthColumnCount = columns?.filter(({ width }) => !width).length;


/* 

"use strict";
const columns = [{ width: 100 }];
const widthColumnCount = columns === null || columns === void 0 ? void 0 : columns.filter(({ width }) => !width).length;

 */

demos

typescript playgorund

https://www.typescriptlang.org/play

JavaScript Bang!

prefix operator

  1. 类型取反
  2. 类型转换成布尔逻辑值
!false
// true
!!false
// false

!0
// true
!!0
// false


refs

https://dev.to/pssingh21/safe-navigation-operator-bang-bang-bang-192j

https://www.cnblogs.com/xgqfrms/p/18222160



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2021-12-14 08:32  xgqfrms  阅读(17)  评论(2编辑  收藏  举报