在 TypeScript 静态类型分为两种,一种是基础静态类型,一种是对象类型。

基础静态类型

基础静态类型非常简单,只要再声明的变量后加一共 : 号,然后加上对应的类型,例如:

const count: number = 666;
const name: string = 'hello'

类似这样常用的基础类型还有 null,undefinde,symbol,booleanvoid 这些都是最常用的基础数据类型;

对象类型

对象类型有以下几种形式,这几种形式我们在TypeScript里叫做对象类型。

  • 对象类型
  • 数组类型
  • 类类型
  • 函数类型
// 对象类型
const teacher: {
  name: string,
  age: number
} = {
  name: 'heyujie',
  age: 18
}
console.log(teacher.name)

// 数组类型
const arrStr: String[] = ["小红", "小结", 'test'];
console.log(arrStr)
// 类类型
class Person { }
const dajiao: Person = new Person();

// 函数类型 getName 是一个函数,这个函数返回值是一个string
const getName: () => string = () => {
  return "大脚";
};
console.log(getName())
View Code

相关文章:

  • 2022-02-26
  • 2022-12-23
  • 2022-12-23
  • 2021-06-07
  • 2021-07-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-28
  • 2021-06-22
相关资源
相似解决方案