当前位置:网站首页>typescript ts 基础知识之类型声明
typescript ts 基础知识之类型声明
2022-07-07 14:49:00 【糖糖246】
定义变量类型、函数参数类型、函数返回值类型
let 变量: 类型
let 变量: 类型 = 值
function fn( 参数:类型,参数:类型): 返回值类型{ ...... }
1. : number 数字
2. : string 字符串
3. :boolean 布尔值
4. : 字面量 限制变量的值就是该字面量的值,eg:let a: 10; a = 11会报错
5. : any 任意类型,相当于对该变量关闭了TS类型检测,不建议使用
let e
e = 'hello' // 可以
let s: string
s = e // 可以6. : unknown 类型安全的any,不能直接赋值给其他变量
let e:unknown
e = true // 可以
e = 'hello' // 可以
let s: string
s = e //报错
//解决
if(typeof e === 'string'){ s = e }
s = e as string //类型断言,用来告诉解析器变量的实际类型 s = <string>e7. : void 空值 没有值(或undefined)常用来设置函数的返回值
8. : never 不能是任何值,永远不会返回结果(throw new Error('报错了'))
9. : object 任意js对象,此种形式不常用,常用以下方式
// {}用来指定对象中包含哪些属性,语法:{属性名:属性值类型,属性名:属性值类型,... }
// 在属性名后边加?,表示属性是可选的
let b: {name: string, age?: number}
b = {name: 'tom', age: 18} //可以
b = {name: 'jack'} //可以
// [propName: string]: any 表示任意类型的属性
let c: {name: string, [propName: string]: any}
c = {name: 'may', age: 12, gender: '女'} //可以
//设置函数结构的类型声明,语法:(形参:类型,形参:类型,...) => 返回值类型
let d: (a:number, b:number) => number
d = function(n1:string, n2: string): number{ return 10 } //报错,参数类型错误10. : array 任意js数组,常用方式:类型[],Array<类型>
let e: string[]
let f: Array<number>11. : tuple 元组,TS新增类型,固定长度数组,语法:[类型,类型,类型,... ]
let h: [string, string]
h = ['hello', 123] // 报错,类型错误
h = ['hello', 'world', '你好'] //报错,个数多了12. : enum 枚举,TS新增类型,将可能的情况列举出来
enum Gender{
Male = 0,
Female = 1
}
let i: {name: string, gender: Gender}
i = {
name: 'hi',
gender: Gender.Male, //数据库中会将gender值存为0
}
console.log(i.gender === Gender.Male)注:
a. 如果变量的声明和赋值是同时进行的,TS可以自动对变量进行类型检测,可以省略类型声明
b. 可以使用 | (或)来链接多个类型(联合类型),结合字面量,可以将变量限制在某几个值之间
c. 声明变量时不指定类型,则TS解析器自动判断变量类型为any,若不知类型可使用unknown类型
d. &表示同时,let j: {name: string} & {age: number},j同时满足含有name和age属性
e. 类型别名 type
type myType = 1|2|3|4|5
let k: myType
let l: myType边栏推荐
- HAVE FUN | “飞船计划”活动最新进展
- 1亿单身男女“在线相亲”,撑起130亿IPO
- Laravel 中config的用法
- 字节跳动Android面试,知识点总结+面试题解析
- logback. XML configure logs of different levels and set color output
- Description of vs common shortcut keys
- 二叉搜索树(特性篇)
- Balanced binary tree (AVL)
- AutoLISP series (1): function function 1
- three. JS create cool snow effect
猜你喜欢

两类更新丢失及解决办法

Talk about the cloud deployment of local projects created by SAP IRPA studio

AutoLISP series (3): function function 3

Personal notes of graphics (1)

【C 语言】 题集 of Ⅹ

字节跳动Android面试,知识点总结+面试题解析

Tragedy caused by deleting the console statement

Description of vs common shortcut keys

Personal notes of graphics (3)

Introduction and use of gateway
随机推荐
pycharm 终端部启用虚拟环境
Personal notes of graphics (4)
在哪个期货公司开期货户最安全?
URL和URI的关系
[designmode] proxy pattern
【MySql进阶】索引详解(一):索引数据页结构
Tragedy caused by deleting the console statement
logback.xml配置不同级别日志,设置彩色输出
Three. JS series (1): API structure diagram-1
laravel怎么获取到public路径
预测——灰色预测
[medical segmentation] attention Unet
Laravel service provider instance tutorial - create a service provider test instance
模仿企业微信会议室选择
iptables只允许指定ip地址访问指定端口
Cesium (4): the reason why gltf model is very dark after loading
How to query the data of a certain day, a certain month, and a certain year in MySQL
二叉搜索树(特性篇)
Personal notes of graphics (1)
掌握这个提升路径,面试资料分享