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. try-catch 异常捕获学习

    #include <iostream> #include <string> #include <vector> #include <stdexcept> ...

  2. 成为一名优秀的Java程序员9+难以置信的公式

    成为一名优秀的Java程序员 成为一名优秀的Java程序员并不重要,但是首先您应该了解基本的编程语言. 好吧,你知道那太好了.我们应该一步一步地精通Java编程,并应遵循所有说明,改进Java的编程逻 ...

  3. Java Web学习之路

    编程基础 1-1 常用数据结构 数组.链表.堆.栈.队列.Hash表.二叉树等1-2 算法思想 算法时间复杂度和空间复杂度的分析计算 1-2 算法思想:递推.递归.穷举.贪心.分治.动态规划.迭代.分 ...

  4. Spring MVC接收参数(Map,List,JSON,Date,2个Bean)(记录一次面试惨状)

    题目Spring MVC 接收参数 MapListDate2个BeanJSON Spring MVC接收参数 -Map Spring MVC接收参数 -List Spring MVC接收参数 -dat ...

  5. sourcetree注册

    http://www.cnblogs.com/xiofee/p/sourcetree_pass_initialization_setup.html

  6. Java编程工具IDEA的使用

    IDEA psvm + Enter 快速构建main方法 sout + Enter 快速打印与句 Ctrl+Shift + Enter,语句完成 Ctrl+F12,可以显示当前文件的结构 Ctrl + ...

  7. cassandra权威指南读书笔记--安全

    认证和授权driver,JMX和cassandra服务器支持SSL/TLS,cassandra节点间也支持SSL/TLS.密码认证器cassandra还支持自定义,可插拔的认证机制.默认的认证器:or ...

  8. 给jekyll博客添加搜索功能

    使用SWIFTYPE为jekyll博客添加搜索引擎 步骤 1.首先去swiftype注册一个账号 2.接着添加自己想要配置的网站地址并为新设定的引擎添加一个名字(非会员只能设置一个引擎). 3.收到验 ...

  9. [CF套题] CF-1163

    CF-1163 传送门 # Penalty A B1 B2 C1 C2 D E F 3 (483) 464 +0 0:06 +1 01:13 +3 01:12 + 01:57 + 01:56 A 第一 ...

  10. Codeforces Round #683 (Div. 2, by Meet IT)【ABCD】

    比赛链接:https://codeforces.com/contest/1447 A. Add Candies 题意 \(1\) 到 \(n\) 个袋子里依次有 \(1\) 到 \(n\) 个糖果,可 ...