Sometimes we want our function arguments to be able to accept more than 1 type; e.g. a string or an array. This lesson will show us how to assign more than 1 type to a variable with Typescript union types and type aliases.

type types = string | boolean | number;

var fn = (sm: types) => sm;

fn("something"); //OK
fn(false); //OK
fn(); //OK
fn([,,]) //Error

Union Type:

var fn = (sm: string | boolean | number) => sm;

But it took many places, so to make it shorter, we use Typoe aliases:

type types = string | boolean | number;

var fn = (sm: types) => sm;

'typeof' and 'instanceof':

type types = string | boolean | number | string[];
var fn2 = (something: types) => {
if(typeof something === "string"
|| typeof something === "boolean"
|| typeof something === "number"){
console.log(something);
} if(something instanceof Array){
let str = "";
something.forEach(s => {
str += s;
})
}
}

Using 'isntaceof', so Typescript understand 'something' is Array type, it will pop up the methods which array can use for.

If we use put unit type as "string" or "object" and try to access the object prop, will throw error:

type stuff = string |{name: string}
var fn3 = (something: stuff) => {
console.log(something.name) // compile error
}

If we put tow object in unit type, but they don't share the same prop:

type objs = {age: number} | {name: string};
var fn4 = (something: objs) => {
console.log(something.age); // compile error
console.log(something.name); // compile error
}

Last if the unit types are objects and share the same prop:

type sharePropObjs = {name: string, age: number} | {name: string, address: string};
var fn4 = (something: sharePropObjs) => {
console.log(something.age); // compile error
console.log(something.address); // compile error
console.log(something.name); // OK
}

To review, the Union type is defined by adding an Or pipe. The Type alias is kind of like a bar, except you're defining a type, not a variable. As of now, we have Type of and Instance of for type cards. Type cards let us differentiate between types and allow TypeScript to know what those types are.

If you Union type Objects with Not Objects, the compiler gets mad. If you Union type Objects without a common parameter, the compiler gets mad. If you Union type Objects with a common parameter, you can access that common parameter.

最新文章

  1. [No00009B]win10快捷键大全
  2. 批处理集锦——(4)2>nul和1>nul是什么意思?
  3. Silverlight自定义控件开发:仪表盘
  4. C语言学习-01第一个C语言程序
  5. [转]使用maven镜像
  6. Oracle 查看相关优化器参数
  7. Andriod docs加载速度慢的问题解决
  8. oc学习之路----代理模式
  9. Delphi过程函数传递参数的八种方式
  10. 《Android进阶》之第一篇 在Java中调用C库函数
  11. idea导入本地maven项目
  12. TypeScript初探
  13. spider随机请求头和ip
  14. VLAN之间通信-三层交换
  15. Git服务器配置和基本使用
  16. dataguard从库删除归档的例子
  17. SQL调优(SQL TUNING)并行查询提示(Hints)之pq_distribute的使用
  18. jetty+mongodb 配置session外部数据库存储
  19. Codeforces 766C - Mahmoud and a Message
  20. win32 MSG 值

热门文章

  1. Enity Framework已经是.NET下最主要的ORM了
  2. js闭包注意事项
  3. 动态引入js代码
  4. 多重背包模板 51Nod 1086
  5. 用node.js从零开始去写一个简单的爬虫
  6. OpenGL核心技术之混合技术
  7. 【翻译自mos文章】OGG的集成捕捉模式支持Oracle database标准版么?
  8. 购买VPS时机房的选择
  9. idea添加自动编译
  10. POJ 1118 Lining Up 直线穿过最多的点数