前端那些事

vuePress-theme-reco chenpeng    2020 - 2021
前端那些事 前端那些事

Choose mode

  • dark
  • auto
  • light
首页
文章目录
  • Browser
  • CSS
  • ES6
  • JavaScript
  • Network
  • TypeScript
  • Vue
  • Vue3
  • Webpack
标签
时间轴
GitHub
author-avatar

chenpeng

85

Article

25

Tag

首页
文章目录
  • Browser
  • CSS
  • ES6
  • JavaScript
  • Network
  • TypeScript
  • Vue
  • Vue3
  • Webpack
标签
时间轴
GitHub
  • TypeScript

    • TypeScript基本概念
    • TypeScript的静态类型
    • TypeScript数组类型注解
    • TypeScript中interface与type的区别

TypeScript的静态类型

vuePress-theme-reco chenpeng    2020 - 2021

TypeScript的静态类型

chenpeng 2020-11-30 TS

# 1.基本类型

// 静态类型
let count: number = 1
1
2

# 2.对象类型

// 对象类型
const cyy: {
    name: string,
    age: number
} = {
    name: 'cyy',
    age: 22
}
1
2
3
4
5
6
7
8

# 3.数组类型

// 数组类型
const persons: {name: string, age: number}[] = [
    {
        name: 'cyy',
        age: 22
    },
    {
        name: 'cy',
        age: 22
    }
]
1
2
3
4
5
6
7
8
9
10
11

# 4.class 类型

// class类型
class Person{}
const p: Person = new Person()
1
2
3

# 5.函数类型

// 函数类型
const add:(x: number, y:number) => number = (x, y) => {return x + y}
const total = add(1, 2)
console.log(total)
1
2
3
4

# 6.自定义静态类型

// 自定义静态类型
interface Person{
    name: string,
    age: number
}

const p: Person = {
    name: 'cyy',
    age: 22
}
1
2
3
4
5
6
7
8
9
10