You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
if(maybe===true){// TypeScript knows that maybe is a boolean nowconstaBoolean: boolean=maybe;// So, it cannot be a stringconstaString: string=maybe;// Type 'boolean' is not assignable to type 'string'.}if(typeofmaybe==="string"){// TypeScript knows that maybe is a stringconstaString: string=maybe;// So, it cannot be a booleanconstaBoolean: boolean=maybe;// Type 'string' is not assignable to type 'boolean'.}
Any
在某些情况,并非所有类型信息都是可用的否则他的类型声明将会耗费很大精力。这在不用typescript 编写的代码或者第三方库经常发生。所以,我们选择退出类型检查,可以用 any 表示这些值:
declarefunctiongetValue(key: string): any;// OK, return value of 'getValue' is not checkedconststr: string=getValue("myString");
letlooselyTyped: any=4;// OK, ifItExists might exist at runtimelooselyTyped.ifItExists();// OK, toFixed exists (but the compiler doesn't check)looselyTyped.toFixed();letstrictlyTyped: unknown=4;strictlyTyped.toFixed();// Error, Object is of type 'unknown'.
any 类型会在对象访问中传递:
letlooselyTyped: any={};letd=looselyTyped.a.b.c.d;// ^ = let d: any
使用any类型会让我们丢失typescript特性--类型安全,我们应该尽量避免使用
Void
void 有点和 any 类型互斥, 表示不是任何类型。经常会在函数没有返回值时使用:
functionwarnUser(): void{console.log("This is my warning message");}
// Not much else we can assign to these variables!letu: undefined=undefined;letn: null=null;letmaybe: unknown=undefined// strictNullChecks = falseletnum:number=undefined
Never
never 类型表示一个总会抛出异常的函数或者死循环的函数,因为它们永远不可能返回值。例外, never 类型不能赋值给任何类型, 其他任何类型也不能赋值给它:
// Function returning never must not have a reachable end pointfunctionerror(message: string): never{thrownewError(message);}// Inferred return type is neverfunctionfail(){returnerror("Something failed");}// Function returning never must not have a reachable end pointfunctioninfiniteLoop(): never{while(true){}}
declarefunctioncreate(o: object|null): void;// OKcreate({prop: 0});create(null);create(42);// Argument of type '42' is not assignable to parameter of type 'object | null'.create("string");// Argument of type '"string"' is not assignable to parameter of type 'object | null'.create(false);// Argument of type 'false' is not assignable to parameter of type 'object | null'.create(undefined);// Argument of type 'undefined' is not assignable to parameter of type 'object | null'.
Type assertions
在某些情况,你知道某个值的类型比当前类型更加具体时, 你需要用到类型断言。 类型断言时一种告诉编译器“trust me, I know what I’m doing.”的感觉, 也类似于其他静态语言的类型转换,但是它不执行任何数据检查和转换。另外它只是在编译阶段有效,对运行是没影响的。
在工作中我们经常用到基础类型,
typescript
提供了和 js 一样的基本类型,比如boolean
,string
,number
,null
等, 并且额外提供了一些有用的辅助类型,比如枚举enum
。在
typescript
中, 类型注解用变量名: 类型
方式表示。Boolean
布尔类型是最简单的类型, 取值为
true/false
。 和js一样, 都用boolean
表示Number
数字类型用
number
表示, 大数字用bigint
表示。 另外可以用0x
,0b
来表示十六和二进制。String
字符串可以用双引号和单引号,类型为
string
可以使用类似ES6的模版字符串
Array
数组类型有两种方式表示, 第一种是数组元素类型紧跟
[]
表示第二种方式是用数组泛型的方式
Array<elemType>
Tuple
当我们需要表示固定数量的不同类型元素的数组时候,就需要用到元组:
当通过访问固定元素时,
ts
会自动推断其类型:访问索引外的元素和赋值也会报错:
Enum
枚举类型是提供一系列的变量来表示数据集的类型, 用
enum
表示:枚举元素默认是从0开始,可以通过设置改变默认值:
同样地,可以手动设置枚举元素的所有值:
可以用过数值获取对应的枚举名:
Unknown
当我们想要表述一个不能确定的类型或者有意接受API返回的所有可能的类型,这时可以用
unknown
类型表示:不能赋值给具体类型的变量:
可以通过比较、
typeof
等类型守卫方式来解决:Any
在某些情况,并非所有类型信息都是可用的否则他的类型声明将会耗费很大精力。这在不用
typescript
编写的代码或者第三方库经常发生。所以,我们选择退出类型检查,可以用any
表示这些值:和
unknown
类型不同,any
类型的变量可以访问任何属性和函数,尽管他们可能不存在,因为typescrpt
不会检查:any
类型会在对象访问中传递:Void
void
有点和any
类型互斥, 表示不是任何类型。经常会在函数没有返回值时使用:定义一个变量为
void
类型是无用的,因为你只能用null
和undefined
类型进行赋值(需要打开--strictNullChecks
配置):Null and Undefined
null
和undefined
类型的值其实用处也不大, 在开启--strictNullChecks
的前提下,只能赋值给unknown
和any
和他们各自的类型, 其中undefined
赋值给void
是个例外。否则,它们可以赋值给任何类型:Never
never
类型表示一个总会抛出异常的函数或者死循环的函数,因为它们永远不可能返回值。例外,never
类型不能赋值给任何类型, 其他任何类型也不能赋值给它:Object
object
类型表示所有非原始类型, 即不是boolean
,number
,string
,bigint
,symbol
,null
和undefined
, 但是一般情况不会用到:Type assertions
在某些情况,你知道某个值的类型比当前类型更加具体时, 你需要用到类型断言。 类型断言时一种告诉编译器“trust me, I know what I’m doing.”的感觉, 也类似于其他静态语言的类型转换,但是它不执行任何数据检查和转换。另外它只是在编译阶段有效,对运行是没影响的。
类型断言有两种方式,第一钟是
as
语法:另一种使用
angle-bracket
语法:其中第一方式优于第二种方式, 并且在JSX使用时,只有用
as
语法The text was updated successfully, but these errors were encountered: