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. 【 APACHE 】 Apache2.4.x版本虚拟主机配置

    今天准备使用apache搭建一个目录浏览的服务,折腾了一下. apache2.4.x以后的版本: Require all granted 代替了apache2.4.x以前版本: Order Allow ...

  2. spring FieldRetrievingFactoryBean

    Spring : 基于XML Schema的配置(一): http://www.tuicool.com/articles/mMjY3uI http://www.cnblogs.com/jifeng/a ...

  3. unity学习笔记1--Space Shooter

    其实我一直觉得我是个模棱两可的人,就计算机这块来说,自己还是想制作游戏什么的,但是又得考虑到现实就业的问题,所以现在自己主要在学安卓和javaweb.现在大概是心血来潮吧,突然想追逐下自己的理想,虽然 ...

  4. Windows+Ubuntu双系统如何设置Windows为第一启动项

    在安装双系统的时候,如果先安装的是Windows然后再安装Ubuntu系统,开机时是以Ubuntu的grub来引导Windows的,而且默认进入Ubuntu系统,下面我们介绍如何更改这个默认项,然后让 ...

  5. Windows终端屏幕显示库Public Domain Curses(PDCurses)使用

    由于Windows没有可用的ncurses库,所以就用PDCurses代替 先放链接 https://sourceforge.net/projects/pdcurses/,下载最新版本 但是我不会编译 ...

  6. Response 部分功能

    设置状态码的方法:    void setStatus(int sc)     void setStatus(int sc, String sm) 设置响应头的方法:    void setHeade ...

  7. django + dropzone.js 上传文件

    1.dropzone.js http://www.dropzonejs.com/ dropzone.js是一个可预览\可定制化的文件拖拽上传,实现AJAX异步上传文件的工具 2.dropzone.js ...

  8. hihocoder 1174 [BFS /拓扑排序判断是否有环]

    hihocoder 1174 [算法]: 计算每一个点的入度值deg[i],这一步需要扫描所有点和边,复杂度O(N+M). 把入度为0的点加入队列Q中,当然有可能存在多个入度为0的点,同时它们之间也不 ...

  9. codeforces #441 B Divisiblity of Differences【数学/hash】

    B. Divisiblity of Differences time limit per test 1 second memory limit per test 512 megabytes input ...

  10. hdu6162(树链剖分)

    hdu6162 题意 给出一颗带点权的树,每次询问一对节点 \((u, v)\),问 \(u\) 到 \(v\) 的最短路径上所有节点权值在 \([c1, c2]\) 区间内的和. 分析 树链剖分,那 ...