博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[TypeScript] Union Types and Type Aliases in TypeScript
阅读量:5077 次
发布时间:2019-06-12

本文共 2439 字,大约阅读时间需要 8 分钟。

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"); //OKfn(false); //OKfn(10); //OKfn([2,2,3]) //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.

转载于:https://www.cnblogs.com/Answer1215/p/5934912.html

你可能感兴趣的文章
BootStrap---2.表格和按钮
查看>>
Linear Algebra lecture 2 note
查看>>
CRC计算模型
查看>>
Ajax之404,200等查询
查看>>
Aizu - 1378 Secret of Chocolate Poles (DP)
查看>>
csv HTTP简单表服务器
查看>>
OO设计的接口分隔原则
查看>>
数据库连接字符串大全 (转载)
查看>>
java类加载和对象初始化
查看>>
对于负载均衡的理解
查看>>
django简介
查看>>
window.event在IE和Firefox的异同
查看>>
常见的js算法面试题收集,es6实现
查看>>
IO流写出到本地 D盘demoIO.txt 文本中
查看>>
Windows10 下Apache服务器搭建
查看>>
HDU 5458 Stability
查看>>
左手坐标系和右手坐标系
查看>>
solr后台操作Documents之增删改查
查看>>
http://yusi123.com/
查看>>
文件文本的操作
查看>>