TypeScript 3.7 RC & Assertion Functions

assertion functions, assert

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

  1. function yell(str) {
  2. assert(typeof str === "string");
  3. return str.toUppercase();
  4. // ~~~~~~~~~~~
  5. // error: Property 'toUppercase' does not exist on type 'string'.
  6. // Did you mean 'toUpperCase'?
  7. }
  8. function assert(condition: any, msg?: string): asserts condition {
  9. if (!condition) {
  10. throw new AssertionError(msg)
  11. }
  12. }

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

type predicate signatures


  1. function isString(val: any): val is string {
  2. return typeof val === "string";
  3. }
  4. function yell(str: any) {
  5. if (isString(str)) {
  6. return str.toUppercase();
  7. }
  8. throw "Oops!";
  9. }

assertion signatures


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

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. WireShark 之 text2pcap

    前言 本来想用 010Editer 的,看到破解教程头都大了,那么就用 WireShark 的 Text2pcap 吧! 正文 打开CMD控制台窗口,转到WireShark安装目录 ,此处可以shif ...

  2. LOJ10081

    USACO 201 Jan. Gold Farmer John 正在一个新的销售区域对他的牛奶销售方案进行调查.他想把牛奶送到 T 个城镇 ,编号为 1 到 T.这些城镇之间通过 R 条道路(编号为 ...

  3. 十一:SpringBoot-事务管理

    SpringBoot-事务管理 1.事务管理简介 1.1 特性:ACID 1.2 隔离问题 1.3 隔离级别 2.Spring事务管理 2.1 顶级接口 2.2 事务状态 2.3 事务定义 1.事务管 ...

  4. (四)Springboot以jar包方式启动、关闭、重启脚本

    Springboot以jar包方式启动.关闭.重启脚本 1.启动 2.关闭 3.重启 4.脚本授权 SpringBoot: nohup java -jar zlkj-data-server-1.0-S ...

  5. Java——单例模式、多线程

    单例模式 单例模式练习 单例模式的分类 懒汉式 懒汉式相关练习 饿汉式 饿汉式相关练习 线程安全 使用双重检测机制实现线程安全的懒汉式 使用静态内部类实现线程安全的单例模式 多线程 多线程的三种方式 ...

  6. Docker --Dockerfile(制作镜像)

    Dockerfile Dockerfile 是一个文本格式的配置文件,用户可以使用 Dockerfile 快速创建自定义的镜像 Dockerfile 常用指令 FROM 作用:指定基础镜像,Docke ...

  7. Java编程工具IDEA的使用

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

  8. Pytest(9)skip跳过用例

    前言 pytest.mark.skip可以标记无法在某些平台上运行的测试功能,或者您希望失败的测试功能 Skip和xfail: 处理那些不会成功的测试用例 你可以对那些在某些特定平台上不能运行的测试用 ...

  9. CF662C Binary Table【FWT】

    CF662C Binary Table 题意: 给出一个\(n\times m\)的\(01\)矩阵,每次可以反转一行或者一列,问经过若干次反转之后,最少有多少个\(1\) \(n\le 20, m\ ...

  10. SPOJ1812 LCS2 - Longest Common Substring II【SAM LCS】

    LCS2 - Longest Common Substring II 多个字符串找最长公共子串 以其中一个串建\(SAM\),然后用其他串一个个去匹配,每次的匹配方式和两个串找\(LCS\)一样,就是 ...