Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript: 数据类型/type/interface #62

Open
leslie1943 opened this issue Nov 24, 2020 · 0 comments
Open

TypeScript: 数据类型/type/interface #62

leslie1943 opened this issue Nov 24, 2020 · 0 comments

Comments

@leslie1943
Copy link
Owner

TypeScript 和 JavaScript

  • TypeScript 是JavaScript的超集
  • TypeScript 在JavaScript的基础上添加类型系统以及完全的支持ES6+语法
  • Angular, Vue3.0将直接支持TypeScript
  • TypeScript 需要编译,JavaScript基本直接被浏览器解析执行

TypeScript 你都用过哪些类型

  • 基本类型,数组类型,函数类型
  • 元组类型
  • 枚举类型

TypeScript 中type和interface的区别

  • 都允许拓展
// interface
interface Name { 
    name: string; 
}
interface User extends Name { 
    age: number; 
}

// type
type Name = { 
    name: string; 
}
type User = Name & { age: number };

// interface  扩展 type
type Name = { 
    name: string; 
}
interface User extends Name { 
age: number; 
}

// type 扩展 interface
interface Name { 
    name: string; 
}
type User = Name & { 
    age: number; 
}
  • type 可以声明基本类型别名,联合类型,元组等类型, interface只能描述一个对象或者函数
  • type 语句中还可以使用 typeof 获取实例的 类型进行赋值
  • interface 能够声明合并, type 不行
interface User {
    name: string
    age: number
}
 
interface User {
    sex: string
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant