[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 ...
 
随机推荐
- Oracle基础 02 临时表空间 temp
			
--查看临时文件的使用/剩余空间 SQL> select * from v$temp_space_header; --查看SCOTT用户所属的临时表空间 SQL> select usern ...
 - gdrive无限网盘挂载systemd文件
			
我的博客新地址:www.liuquanhao.com --------------------------------------------------------------------- 首先应 ...
 - Win 32平台SDK中的文件操作
			
读取文件: HANDLE hFile ; // 声明文件操作内核对象句柄 hFile = CreateFile(, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL ...
 - springboot 返回json格式数据的时间格式配置
			
#时间戳统一转换 spring.jackson.date-format=yyyy-MM-dd HH:mm:ss spring.jackson.time-zone=GMT+8 NOTE:time-zon ...
 - java 获取当前系统时间
			
Java的Date获取时间函数都是deprecated 可以使用: https://stackoverflow.com/questions/5175728/how-to-get-the-current ...
 - JS动态计算移动端rem的解决方案
			
首先介绍下rem 说起rem就的说px,em: PX为单位 在Web页面初期制作中,我们都是使用“px”来设置我们的文本,因为他比较稳定和精确.但是这种方法存在一个问题,当用户在浏览器中浏览我们制作的 ...
 - (5)python 字符串和输入输出
			
一.字符串转义 字符串可以包含任何字符可以用单引号也可以用双引号 a='hello' a="hello" 如果字符串中存在单引号,可以用双引号里包含单引号的方式 a="I ...
 - 洛谷 P1308 统计单词数【string类及其函数应用/STL】
			
题目描述 一般的文本编辑器都有查找单词的功能,该功能可以快速定位特定单词在文章中的位置,有的还能统计出特定单词在文章中出现的次数. 现在,请你编程实现这一功能,具体要求是:给定一个单词,请你输出它在给 ...
 - Android学习--跨程序共享数据之内容提供其探究
			
什么是内容提供器? 跨程序共享数据之内容提供器,这是个什么功能?看到这个名称的时候最能给我们提供信息的应该是“跨程序”这个词了,是的重点就是这个词,这个内容提供器的作用主要是用于在不同的引用程序之间实 ...
 - 51nod 循环数组最大子段和(动态规划)
			
循环数组最大子段和 输入 第1行:整数序列的长度N(2 <= N <= 50000) 第2 - N+1行:N个整数 (-10^9 <= S[i] <= 10^9) 输出 输 ...