TypeScript 3.7 RC & Assertion Functions

assertion functions, assert

https://devblogs.microsoft.com/typescript/announcing-typescript-3-7-rc/#assertion-functions

function yell(str) {
assert(typeof str === "string"); return str.toUppercase();
// ~~~~~~~~~~~
// error: Property 'toUppercase' does not exist on type 'string'.
// Did you mean 'toUpperCase'?
} function assert(condition: any, msg?: string): asserts condition {
if (!condition) {
throw new AssertionError(msg)
}
}

function assertIsString(val: any): asserts val is string {
if (typeof val !== "string") {
throw new AssertionError("Not a string!");
}
} // Here asserts val is string ensures that after any call to assertIsString, any variable passed in will be known to be a string. function yell(str: any) {
assertIsString(str); // Now TypeScript knows that 'str' is a 'string'. return str.toUppercase();
// ~~~~~~~~~~~
// error: Property 'toUppercase' does not exist on type 'string'.
// Did you mean 'toUpperCase'?
}

type predicate signatures


function isString(val: any): val is string {
return typeof val === "string";
} function yell(str: any) {
if (isString(str)) {
return str.toUppercase();
}
throw "Oops!";
}

assertion signatures


function assertIsDefined<T>(val: T): asserts val is NonNullable<T> {
if (val === undefined || val === null) {
throw new AssertionError(
`Expected 'val' to be defined, but received ${val}`
);
}
}

refs

https://www.cnblogs.com/xgqfrms/tag/TypeScript 3.7 RC/



xgqfrms 2012-2020

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


TypeScript 3.7 RC & Assertion Functions的更多相关文章

  1. TypeScript 3.7 RC & Optional Chaining

    TypeScript 3.7 RC & Optional Chaining https://devblogs.microsoft.com/typescript/announcing-types ...

  2. TypeScript 3.7 RC & Nullish Coalescing

    TypeScript 3.7 RC & Nullish Coalescing null, undefined default value https://devblogs.microsoft. ...

  3. [TypeScript] Define Custom Type Guard Functions in TypeScript

    One aspect of control flow based type analysis is that the TypeScript compiler narrows the type of a ...

  4. TypeScript 2.0候选版(RC)已出,哪些新特性值得我们关注?

    注:本文提及到的代码示例下载地址 - Runnable sample to introduce Typescript 2.0 RC new features 作为一个Javascript的超集, Ty ...

  5. TypeScript 面试题汇总(2020 版)

    TypeScript 面试题汇总(2020 版) TypeScript 3.9 https://www.typescriptlang.org/zh/ TypeScript 4.0 RC https:/ ...

  6. grep与正则表达式的使用

    正则表达式以及grep的使用 grep是一种文本过滤工具(模式:pattern)基本使用用法如下: grep [option] PATTERN FILE grep [OPTIONS] [-e PATT ...

  7. 06 Frequently Asked Questions (FAQ) 常见问题解答 (常见问题)

    Frequently Asked Questions (FAQ) Origins 起源 What is the purpose of the project? What is the history ...

  8. 运行级别(run level)

    inittab是很多linux版本的启动脚本.Linux在完成核内引导以后,就开始运行init程序,它的进程号是1,是所有其他进程的起点.init需要读取/etc/inittab,该文件告诉init在 ...

  9. /etc/fstab 官方文档

    1什么是fstab 2fstab文件示例 3fstab 文件组成 4文件系统标识 4.1Kernel naming 4.2UUID 4.3Label 5建议 5.1atime 参数 5.2tmpfs ...

随机推荐

  1. 洛谷 P3704 SDOI2017 数字表格

    题意: 给定两个整数 \(n, m\),求: \[\prod_{i = 1} ^ n \prod_{j = 1} ^ m \operatorname{Fib}_{\gcd\left(n, m\righ ...

  2. Spring MVC—拦截器,文件上传,中文乱码处理,Rest风格,异常处理机制

    拦截器 文件上传 -中文乱码解决 rest风格 异常处理机制 拦截器 Spring MVC可以使用拦截器对请求进行拦截处理,用户可以自定义拦截器来实现特定的功能,自定义的拦截器必须实现HandlerI ...

  3. snmp协议 及snmpwalk

    推荐阅读: snmp及工具:https://www.jianshu.com/p/dc2dc0222940 snmp协议详解:https://blog.csdn.net/shanzhizi/articl ...

  4. MySQL安全审计(init_connect)

    1.常规安全 在说审计之前我们先提一点一般我们常用的MySQL的安全注意事项. 指定完善的MySQL安全流程 用户授权邮件备注 每个人对应权限均需留底 所有用户非管理员及特殊账户,均精细化授权 2.s ...

  5. 【从零开始撸一个App】RecyclerView的使用

    目标 前段时间打造了一款简单易用功能全面的图片上传组件,现在就来将上传的图片以图片集的形式展现到App上.出于用户体验考虑,加载新图片采用[无限]滚动模式,Android平台上我们优选Recycler ...

  6. Codeforces Round #651 (Div. 2) E. Binary Subsequence Rotation(dp)

    题目链接:https://codeforces.com/contest/1370/problem/E 题意 给出两个长为 $n$ 的 $01$ 串 $s$ 和 $t$,每次可以选择 $s$ 的一些下标 ...

  7. HDU - 4300 Clairewd’s message (拓展kmp)

    HDU - 4300 题意:这个题目好难读懂,,先给你一个字母的转换表,然后给你一个字符串密文+明文,密文一定是全的,但明文不一定是全的,求最短的密文和解密后的明文: 题解:由于密文一定是全的,所以他 ...

  8. 【poj 1061】青蛙的约会(数论--拓展欧几里德 求解同余方程)

    题意:已知2只青蛙的起始位置 a,b 和跳跃一次的距离 m,n,现在它们沿着一条长度为 l 的纬线(圈)向相同方向跳跃.问它们何时能相遇?(好有聊的青蛙 (΄◞ิ౪◟ิ‵) *)永不相遇就输出&quo ...

  9. poj1821 Fence(dp,单调队列优化)

    题意: 由k(1 <= K <= 100)个工人组成的团队应油漆围墙,其中包含N(1 <= N <= 16 000)个从左到右从1到N编号的木板.每个工人i(1 <= i ...

  10. Tomacat目录以及服务器配置文件信息

    一. 1.Tomacat的启动: 在我的windows10中我下载的是8.5版本的tomacat,我就是通过".sh"文件来打开和关闭tomacat 要打开.sh文件还需要 这个G ...