00:00
好,那么我们下边呢,来说一说这个联合类型啊。呃,联合类型呢,在TS当中也是属于基础类型,它这玩意可以表示说取值的时候可以为多种类型中的一种,然后下边有个需求啊,咱们来一起看一下联合类型,并且通过一个例子来说明这个联合类型是怎么回事啊。我把它拿过来。那这个呢,也是。加个注释啊,定义一个函数,得到一个数字或字串值的字串形式啊,什么意思来看看啊,就说我来一个函数,比如说get什么呢?Get这个string吧。括号,然后呢,你将来给我传入一个数据,这个数据可以是数字类型的,但同时也可以是一个字符串类型的,这我就尴尬了是吧,那怎么写呢?直接写个竖线。String,那这个时候将来外边去调get string的时候就可以这么写了,来看啊。can.log括号get straight,诶然后呢,我传入一个123大家看可以,我传入一个字符串的123家看也可以是吧,而且它也有提示啊,那我写半括号的时候大家看这。
01:08
嗯,Str冒号string啊,竖线number,就是说我将来传的这个参数可以是字串类型,也可以是数字类型,这是没有问题的啊,比如说我写个123是吧,好,然后呢,我希望这个函数的反回值是一个纵串我加的啊,那怎么写的,比如说return s tr.to s tr NG two率是吧,我去保存一下啊,咱们来看一下。输出的结果呢,是123是吧,好,那如果说我在这个位置写的是这样的东西,诶123再保存一下,我们再来看。好,大家看,这就是123也没有问题是吧?哎,这个就是所谓的联合类型,一个变量它的数据类型可以是多种或者说多个啊,这都行,但一般情况下我们是放在函数里边去书写的是吧?函数里边有参数,参数呢,将来别人在调用函数传递的时候,传入到这个实参,它的类型可以是有多个选择的,这个就是所谓的联合类型。
02:06
那好,咱们接着这个例子往后去说啊,呃,后边还有一个叫什么呢。看这个叫做类型断言的是吧,好注意看啊。这有一个需求,我们在提出来这个需求的时候,就会涉及到这个类型断言的问题啊,咱们来看啊。把这个需求呢也拿过来。我放在这儿。然后呢,我把上面这个代码复制一份啊,复制一份呢,我把整个这个代码就出示掉了,不要了啊,咱们来看说定义一个函数,得到一个数字或字符串值的长度。如果说要是仅仅返回字符串类型,那我to string调这个方法就行了,现在问题是要返回一个长度,我们来看怎么写啊。那我就这么写了,那直接return s tr点。出STG括号,点L。
03:00
字符串长度嘛,那这个位置应该是一个number的对不对,然后呢,我在这下边canl.log括号get string。括号我们来看一下啊,说你给我传入一个字串或者数字都行,并且返回的是一个数字类型是吧?好,那我在这个里边直接写上个123啊12345吧,保存一下,我们再来看一下。刷新。K,有五个是吧,没有问题,然后呢,我再写上一个这个。代号。0123456是吧,保存一下啊。好,我们再看是七个也没有问题是吧,这样呢,我把我刚才写的代码也放在这儿啊,12345是吧,好。他们两个。都传进来啊,这个呢我先注释掉,这个呢我也先注释掉,好,但是这个时候就有一个问题了,什么问题呢?说你这个还要能够得到一个数字,或者是字符串值的这个长度,我除了这种写法以外,我还可以怎么写呢?我应该这么写。
04:03
起码我应该去判断一下。为啥说要判断一下呢?如果说我这个str传进来的就是字符串,那么大家想我有必要string吗?是不是没有啊,如果说我str传进来的是number,那我这个number可能需要to string一下,所以说此时上面这个代码就不是特别的合理啊,为什么?因为如果说str本身就是什么呢?String类型。逗号,那么是没有必要调用什么呢?To string的这个方法的是不是?哎,所以说我们可以这么写,如果说S tr.Les存在。是吧?诶,Else,上边是说S tr.Les存在吗?是吧?但如果存在逗号也就说明啊,不用逗号也就说明,也就说明什么呢?Str是什么呢?String类型的对不对?那我就可以直接return s tr DR nice了,是吧?那else此时说明什么呢?Str是number类型。
05:19
那如果是number类型,我才有必要S加点to string,然后再去调。这个length对不对,但是现在上面报错了,我们来看一下。为什么呢?说类型string或number上不存在属性length。类型单上边不存在属性代词,也说此时他不知道你的str到底是不是字符串类型。是吧,但是他可能知道这个东西可能是一个number类型,所以说就出现了这样的一个问题是吧,我们来看怎么解决啊。这又是一个新的东西,叫类型断言。啊,什么意思呢?说通过类型断言这种方式可以告诉编译器,相信我,我知道自己在干什么。
06:02
换言之,也就是说现在我们这个位置报错,是因为TS有对应的编译器,那个编译器在我们写代码的时候,通过静态代码的这个分析知道什么呢?你这个str可能是有问题的,所以说他就给我们提出来这样的错误消息。那我们如果有类型断言的这个东西出现之后,那编译器可以很清楚的知道当前我这个str就应该是什么类型的,就不会出现这种错误提示信息的。然后我们来看啊,说通过类型断言这种方式告诉编系,现在编系可能不知道吗?是吧,现在告诉编系,相信我,我知道自己在干什么,那类型断言好比是其他类型语言里边的什么类型转换,但是不进行特殊的数据检查和结构,它没有运行时的影响,只是编译阶段起了作用。那你说啊。TS里边给我们整出来一个叫类型断言的东西,它有两种形式,其一是间括号的语法,另外一个就是as,什么意思呢?好。
07:01
我们希望这个str是一个字符串的时候做这个判断,对吧,我们只需要在这个位置。写上一个四倍。然后呢,注意看。这个位置写上一个string啊,注意啊,是应该把它给包裹起来啊。包裹起来之后啊,先成一个磁针,此时这个str就是一个什么类型的,是一个字串体型了。哎,类型断言断定你当前的str变量是字符串的型,那你去点就没有问题了是吧,所以说这个位置我们通过这种类型断言的方式。可以解决这个什么呢。错误的提示信息,然后我去保存啊,当然我们去输出一下。打开啊。保存一下。然后呢,我们再去打开这个结果,大家可以看一下,一个是五,一个是七是没有问题的是吧,然后这个代码要比我们之前写一行代码更加的好了,虽然说比上面那个代码麻烦,但是呢,我们又学了一个新的东西,叫做类型断言啊。
08:01
啊。然后呢,它的这个语法实际上就是告诉这个编译器啊,我知道我自己是什么类型的,也知道自己在干什么是吧,告诉编译器编那个错误提示信息是编译器给我们提示的,他这种写法是告诉编译器是吧,我这东是字串的腰,你别给我报那个错了啊,然后呢,结束。这个类型断言的这个语法方式啊,一是这写这种监块的方式,里边写的是类型,后边跟的是变量名。啊,这是方式一,然后呢,这个类型断言的语法方式二,A类型断言断言的语法方式二是这么写的,是一个值,然后is后边这个类型,然后呢,我们来看一下啊。呃,这个也比较简单,直接写上一个什么呢?X as,然后string这种方式啊,比如说什么呢,当前我的这个位置啊,我把这个代码去掉,这么写return括号,当前str,然后as什么的string类型。
09:15
然后呢,第2LES咱们来看一下啊,就相当于说我把str已经变成了这种字符串就行了,然后呢,我就可以直接调用这个lengths的属性去使用了,我去保存啊,我们来看一下这个结果。和之前是一样的,是没有问题的,这个就是所谓的类啊,这个类型断言啊,可以很好的告诉编辑,我知道我自己是啥,然后呢,你就不要再给我报这种错误了啊,还有最后一个问题叫做什么叫做类型推断。TS会在没有明确的指定类型的时候推测出一个类型来。也就是说什么呢,这种情况。类型推断什么情况,比如说我let。嗯,随便来一个吧,TST吧,等于。100是吧,然后我can.log我们来看TST。
10:02
我们鼠标放到这个TST上面啊,告诉我们是什么类型,Number类型,这个就是所谓的类型推断,当然我去保存它,咱们来看一下结果。OK,输出100没问题是吧?好,那此时呢,我把这个TST等于,比如说飞哥好帅,哎,大家看,直接报错啊,边系不允许我们把鼠标放在这,说什么不能将类型string分配给类型number。那这个就是所谓的类型推断,什么时候会出现这个问题呢?这就是我们这个笔记里边所提的啊,说什么呢,在没有明确指定类型的时候,会推测出来一个类型。就是所谓的类型推断啊。好,我把这个呢,先干掉它。啊,当然这行代码肯定是有问题的啊,我就是我就把它啊,还是换成一个吧,换成一个这个换成一个小甜甜好帅啊好。然后还有另外一个是吧,如果说我定义了一个变量没有赋值,那这个变量是什么类型呢?咱们来看一下啊。
11:00
再来一个let t2吧,好,然后。Can,诶多了can.log括TXT2,然后我把鼠标放到这来啊,咱们来看一下是什么类型,In类型是吧,I那类型是不是就可以复制给任意的数据了,比如说我先给个100是吧,保存一下。我们来看。结果就是100是吧,好,那这个时候我再给他TST2等于啊,比如说是吧,帅阳好帅哦,保存一下,我们再来看一下。结果是衰好衰,这个就是I的类型啊。推断出来any类型,好,上面这个推断出来是什么呢?Number类型啊,这个就是所谓的类型推断啊,注意啊,咱们刚才讲了几个东西,一个是联合类型是吧,当我的一个函数里边这个的这个参数。可能是需要将来传入不同类型数据的时候,那么我们可以使用联合类型是吧?如果说将来我的变量要调用什么属性的时候,但是又不确定当前这个变量的类型是什么,我们可以使用什么呢?类型断言。
12:10
如果说我们定义的变量一开始没有明确指出该变量的类型,那么这个变量根据它第一次初始化的值来推断这个变量是什么类型的,或者说变量声明了没有赋值,它会推断出来当前这个变量应该是属于N类型的啊。以上所有的内容都是关于TS里边的基础类型的一个说明啊。
我来说两句