当前位置:网站首页>TS初次使用、ts类型
TS初次使用、ts类型
2022-07-01 23:20:00 【qq_46302247】

ts类型申明
//参数a,b,函数返回值的类型都申明为number类型,如果传参中有非number类型的数据,则会显示红色波浪线
function sum(a: number, b: number): number {
return a + b
}
console.log(sum(1, 9)); //10
//此处的a相当于const定义的,a的值永远为10
let a: 10;
//b的值只能为 'male'或者'female'
let b: 'male' | 'female'
//b的值只能为 boolean类型或者string类型
let c: boolean | string
//any 表示任意类型,可以给d赋上任意类型的值(相当于对d关闭了TS的类型检测)
let d: any
d = true
//等同于 let d
let s: string
s = d // d是any类型,s也变成了any类型(所以开发中尽量避免使用any)
//unknown 表示未知类型,也可以给d赋上任意类型的值
let e: unknown
e = 'hellow'
let s: string
//s = e // 语法检测不通过,因为s是string类型,e是unknown类型,它们类型不用,不允许直接赋值
//如果就是想把e的值赋给s,有两种解决办法
方法一:
if(typeof e === "string") {
s = e
}
方法二:
//使用类型断言
s = e as string
或者:
s = <string>e
// void 用来表示空,以函数为例,就表示没有返回值
function fn(): void {
}
function fn(): void {
//return undefined
//return;
//return null
}

函数一旦报错就立即终止,never用的很少
// {} 用来指定对象类型
let a: {
name: string}
// a中有且只有name属性,且name值必须为string类型
a = {
name: '猪八戒'}
let b: {
name: string, age?: number}
// b中必须有name属性,且name值必须为string类型;age可有可无;
b = {
name: '猪八戒'}
或者
b = {
name: '猪八戒', age: 18}
let c: {
name: string, [propName: string]: number}
// b中必须有name属性,且name值必须为string类型;剩余属性的个数,属性名不做要求,但是必须是number类型
c = {
name: '猪八戒', haha: 12, xixi: 99}
// () => {} 用来指定函数对象类型
let d: (a: number, b: number)=>number
//函数d只接收两个number类型的参数,且返回值必须为number类型
d = function(n1: number, n2: number): number {
return n1 + n2
}
// [] 用来指定对数组类型
let a: number[]
等同于
let a: Array<number>
a = [1,3,6,5,9]
//tuple是元组(ts新增),元组表示固定长度的数组
let a: [string, number]
//数组a中只有两个元素,第一个为string类型,第二个为number类型
a = [1, '2']
//enum是枚举(ts新增)
enum Gender{
male,
female
}
let a: {
name: string, gender: Gender}
a = {
name: '猪八戒', gender: Gender.male}
// & 表示同时
let a: {
name: string} & {
age: number}
等同于
let a: {
name: string, age: number}
//对象a中必须同时有name和age,且类型为指定类型
a = {
name: '猪八戒', age: 18}
//类型的别名
type myType = 1 | 2 | 3
let a: myType
let c: mType
a = 1, c = 3
// a、c的值为1或2或3,都行
边栏推荐
- from pip._ internal. cli. main import main ModuleNotFoundError: No module named ‘pip‘
- 证券开户选哪个证券公司比较好,哪个更安全
- Glass mosaic
- SWT / anr problem - SWT causes kernel fuse deadlock
- 2022安全员-C证考试题模拟考试题库及模拟考试
- VIM color the catalogue
- The difference between timer and scheduledthreadpoolexecutor
- What category does the Internet of things application technology major belong to
- 2022 R1 fast opening pressure vessel operation test questions and answers
- 2021 RoboCom 世界机器人开发者大赛-高职组初赛
猜你喜欢

Stm32f030f4 drives tim1637 nixie tube chip

Current situation and future development trend of Internet of things

"35 years old, the boss of the company, with a monthly salary of 20000, give away takeout": the times abandoned you, not even saying goodbye

De PIP. Interne. CLI. Main Import main modulenotfounderror: No module named 'PIP'

notBlank 和 notEmpty

Istio, ebpf and rsocket Broker: in depth study of service grid

Notes to problems - file /usr/share/mysql/charsets/readme from install of mysql-server-5.1.73-1 glibc23.x86_ 64 c

Glass mosaic

Future trend and development of neural network Internet of things

Zhao Fuquan: to ensure supply in the short term, we should build a safe, efficient and resilient supply chain in the long term
随机推荐
The difference between timer and scheduledthreadpoolexecutor
Similarities and differences between the defined identity execution function authid determiner and PostgreSQL in Oracle
13 MySQL-约束
Redis AOF日志
dat. GUI
mysql binlog的清理
Istio, ebpf and rsocket Broker: in depth study of service grid
What is the mosaic tailgate?
Applet form verification encapsulation
【无标题】
2022 R1 fast opening pressure vessel operation test questions and answers
Linux基础 —— CentOS7 离线安装 MySQL
Concepts of dictionary, hash table and array
De PIP. Interne. CLI. Main Import main modulenotfounderror: No module named 'PIP'
Zero foundation tutorial of Internet of things development
mt管理器测试滑雪大冒险
The third part of the construction of the defense system of offensive and defensive exercises is the establishment of a practical security system
What professional classification does the application of Internet of things technology belong to
2022年R1快开门式压力容器操作考题及答案
MT manager test skiing Adventure