[TypeScript] Use the TypeScript "unknown" type to avoid runtime errors
The "any" type can be very useful, especially when adding types to an existing JavaScript codebase, but it can also lead to lots of runtime errors, as it provides no type-checking. The "unknown" type on the other hand, restricts developer from calling anything on it or assigning to any other type. The only way to use "unknown" variables is to apply logic flow type narrowing or casting, as that's the only way TypeScript can trust that it's dealing with a correct type. The "unknown" type is useful when building services or APIs that can return data of an unknown shape, so we might want to stop other developers from using it in potentially dangerous ways until it's been narrowed down to a specific type.
"any" type can pass in the compile phases without any problem, but will throw error in runtime.
We can use new type: 'unknown'.
For exmaple, we have code:
interface IComment {
date: Date;
message: string;
}
interface IDataService {
getData(): unknown;
}
let service: IDataService;
const response = service.getData();

You cannot directly use 'unknown' type, we have to tell Typescipt what type it is:
if(typeof response === 'string') {
console.log(response.toUpperCase());
} else if(isComment(response)){
response.date;
}
function isComment(type: any): type is IComment {
return (<IComment>type).message !== undefined && (<IComment>type).date !== undefined;
}
[TypeScript] Use the TypeScript "unknown" type to avoid runtime errors的更多相关文章
- nodejs + typescript + koa + eslint + typescript eslint + prettier + webstorm
ESLint 安装 yarn add -D eslint 生成配置文件 yarn eslint --init cli 选项 How would you like to use ESLint? To c ...
- 由于源码使用是c\c++与oc混编导致Unknown type name 'NSString'
今天看到个问题,编辑工程提示Unknown type name 'NSString',如下图 解决方案三: 将Compile Sources As 改为 Objective-C++
- libavcodec/dxva2.h:40:5: error: unknown type name 'IDirectXVideoDecoder'
gcc 4.9.2 编译 ffmpeg-git-1aeb88b 是出现如下错误 > FFmpeg fails to make with: > > CC libavcodec/dxva ...
- Unknown type name “CGFloat
一.编绎显示Unknown type name “CGFloat” 错误解决方法 将Compile Sources As 改为 Objective-C++ 二.如果是extern const引起的. ...
- Unity3d:Unknown type 'System.Collections.Generic.CollectionDebuggerView'1
问题描述:如图,在调试状态下说:Unknown type 'System.Collections.Generic.CollectionDebuggerView'1<ignore_js_op> ...
- [TypeScript] Stopping a TypeScript Build When Errors Are Found
TypeScript will always compile even if there are ridiculous errors in your project. This lesson show ...
- iOS开发——导入第三方库引起的unknown type name 'NSString'
今天加入SVProgressHUD的第三方库的时候报了24个错误( too many errors emitted, stopping now),都是 expected identifier or ' ...
- unknow Unknown type name 'NSString'
转载:geweb 今天看到个问题,编辑工程提示Unknown type name 'NSString',如下图 导致出现异常的原因是是因为工程中添加了ZipArchive(第三方开源解压缩库) 一般情 ...
- Unknown type name 'NSString' 解决方案
今天看到个问题,编辑工程提示Unknown type name 'NSString',如下图 导致出现异常的原因是是因为工程中添加了ZipArchive(第三方开源解压缩库) 一般情况下出现“Unkn ...
随机推荐
- mysql常用函数示例
CREATE TABLE `orders` ( `OrderId` INT(10) NOT NULL AUTO_INCREMENT COMMENT '编号', `ProductName` VARCHA ...
- css用法(持续更新ing)
*:选择所有节点 #container:选取id为container的节点 .container:选取所有class包含container的节点 li a:选取li下的所有a节点 ul +p:选取ul ...
- Selenium2+python自动化31-生成测试报告【转载】
前言 最近小伙伴们总有一些测试报告的问题,网上的一些资料生成报告的方法,我试了都不行,完全生成不了,不知道他们是怎么生成的,同样的代码,有待研究. 今天小编写一下可以生成测试报告的方法.个人觉得也是最 ...
- docker从零开始 存储(五)存储驱动介绍
关于存储驱动程序 要有效地使用存储驱动程序,了解Docker如何构建和存储镜像以及容器如何使用这些镜像非常重要.您可以使用此信息做出明智的选择,以确定从应用程序中保留数据的最佳方法,并避免在此过程中出 ...
- Windows和Ubuntu平台Android +JAVA 环境搭建
NOTE 测试的时候,尤其是移动端的测试,需要搭建JAVA和Andriod环境: appium和macaca都需要这两个环境: Q&A Macaca doctor 发现没有platforms这 ...
- [linux]多线程下载
axel -n 10 -o /tmp/ http://soft.vpser.net/lnmp/lnmp0.7-full.tar.gz # 10是线程数
- CF988 D. Points and Powers of Two【hash/数学推理】
[链接]:CF [题意]:从一堆数中选一个最大子集,使得任意两个数相减的绝对值都是2的幂. [分析]:首先很难的一点,需要想到子集最多只能有三个,四个及以上的子集一定不存在(可以证明).当有三个元素时 ...
- HDU 6206 Apple【计算几何+高精度Java】
Problem Description Apple is Taotao's favourite fruit. In his backyard, there are three apple trees ...
- python requests的安装与简单运用(转)
http://www.cnblogs.com/fightformylife/p/4134986.html http://cn.python-requests.org/zh_CN/latest/ htt ...
- 【字符串哈希】bzoj3098 Hash Killer II
教育我们做Rabin-Karp的时候一定要把模数取大?还是上溢好了. #include<cstdio> #include<cstdlib> using namespace st ...