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的更多相关文章

  1. 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 ...

  2. 由于源码使用是c\c++与oc混编导致Unknown type name 'NSString'

    今天看到个问题,编辑工程提示Unknown type name 'NSString',如下图 解决方案三: 将Compile Sources As 改为 Objective-C++

  3. 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 ...

  4. Unknown type name “CGFloat

    一.编绎显示Unknown type name “CGFloat”  错误解决方法 将Compile Sources As 改为 Objective-C++ 二.如果是extern const引起的. ...

  5. Unity3d:Unknown type 'System.Collections.Generic.CollectionDebuggerView'1

    问题描述:如图,在调试状态下说:Unknown type 'System.Collections.Generic.CollectionDebuggerView'1<ignore_js_op> ...

  6. [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 ...

  7. iOS开发——导入第三方库引起的unknown type name 'NSString'

    今天加入SVProgressHUD的第三方库的时候报了24个错误( too many errors emitted, stopping now),都是 expected identifier or ' ...

  8. unknow Unknown type name 'NSString'

    转载:geweb 今天看到个问题,编辑工程提示Unknown type name 'NSString',如下图 导致出现异常的原因是是因为工程中添加了ZipArchive(第三方开源解压缩库) 一般情 ...

  9. Unknown type name 'NSString' 解决方案

    今天看到个问题,编辑工程提示Unknown type name 'NSString',如下图 导致出现异常的原因是是因为工程中添加了ZipArchive(第三方开源解压缩库) 一般情况下出现“Unkn ...

随机推荐

  1. Git-ssh登录github

    生成你的ssh-key $ ssh-keygen -t rsa -b 4096 -C  "SaphhireCastle@163.com" 默认目录为:/Users/you/id_r ...

  2. python Nosql-redis 连接、管道

    非关系型数据库和关系型数据库的差别: 非关系型数据库的优势: 性能NOSQL是基于键值对的,可以想象成表中的主键和值的对应关系,而且不需要经过SQL层的解析,所以性能非常高. 可扩展性同样也是因为基于 ...

  3. [译]lambda表达式对 SAM (单个抽象方法类)type的处理方式

    在阅读Venkat Subramaniam的著作<Functional Programming in Java> 之后,方法模式和lambda完美结合让我印象深刻. 这种模式经常用作数据源 ...

  4. Servlet response原理

    首先web服务器 接受到http请求后转交给相应的servlet进行处理这个过程可以他通过配置web.xml来进行确定,然后web服务器将相应的信息封装到request和response对象,由相应的 ...

  5. c# WinForm窗体编程中对窗体程序设置快捷键

    c# WinForm窗体编程中对窗体程序设置快捷键http://www.cnblogs.com/bison1989/archive/2011/09/19/2180977.html /// <su ...

  6. [BZOJ1176][Balkan2007]Mokia cdq+树状数组

    1176: [Balkan2007]Mokia Time Limit: 30 Sec  Memory Limit: 162 MBSubmit: 3134  Solved: 1395[Submit][S ...

  7. 安装tomcat7.0.82

    下载安装tomcat mkdir /tools cd /tools wget http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.82/bin/ap ...

  8. 51nod 1183 编辑距离【线性dp+类似最长公共子序列】

    1183 编辑距离 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 编辑距离,又称Levenshtein距离(也叫做Edit Distance),是指两个 ...

  9. debug 英文翻译

    declaration of 'int a' shadows a parameter :函数参数里,已经有这两个名称的变量了,指定义了同名的参数,造成了隐藏. expected primary-exp ...

  10. 2018 ACM-ICPC 沈阳网络赛

    Problem A Problem B Problem C Problem D Problem E Problem F Problem G Problem H Problem I Problem J ...