课程名称:NestJS 入门到实战 前端必学服务端新趋势
课程章节: 第1章
课程讲师:Brian
课程内容
// tuple -> 元组 -> 固定类型 + 长度得数组
const teacherInfo: [string, string, number] = ['toimc','male',18]
// enum -> 枚举 -> 罗列出来得所有可能情况 -> 常量
// 性别 男 女
enum Direction {
Up,
Down,
Left,
Right
}
enum Gender {
Male,
Female
}
console.log(Gender.Male)
console.log(Gender[0])
console.log(Gender[1])
console.log(Gender[2])
enum Direction1 {
Up=60,
Down,
Left,
Right
}
console.log(Direction1.Up)
console.log(Direction1.Down)
console.log(Direction1.Left)
console.log(Direction1.Right)
console.log(Direction1[100])// 接口-> ts最重要的概念 -> 定义任意的结构或者类型
// 接口-> ts最重要的概念 -> 定义任意的结构或者类型
interface publicPoint {
x: number;
y: number;
z: number;
}
interface Point extends publicPoint {
a?: number,
}
const myPoint: Point = {x:1,y:4,z:0}
// 定义函数
interface Func {
(num1: number, num2:number): number
}
const addFunc:Func= (arg1, agg2)=> arg1+agg2
// 索引类型
interface Role {
[id: number]: string
}
const role: Role = ['super_admin', 'admin']
console.log(role)
// console.log(role.length)
// 当定义了索引类型后,数组的length方法 将不存在,包括Array原型链上的其他方法也不存在
const role1: Role = {
0: "super_admin",
1: "admin",
6: "user"
}
console.log(role1)
// 绕开多余属性检查
interface MyType {
color: string
[prop: string]: any
}
const getTypes = (myType: MyType) => {
return `${myType.color}`
}
// 1.类型断言 as MyType 屏蔽掉多余的 type num
getTypes({
color: 'red',
type: 'color',
num: 0
} as MyType)
// 2.索引签名 添加 [prop: string]: any 后 就不需要再加 as
// getTypes({
// color: 'red',
// type: 'color',
// num: 0
// })
// 3.类型兼容 不推荐
const option = {color: 'yellow', size: 12}
const getTypes1 = ({color}: MyType) => {
return `${color}`
}class Person {
// public - 公共的
// protected - 允许在类内及继承的子类中使用
// private - 只允许在类内使用
private name = 'toimc' // 默认public
protected name1 = 'toimc' // 被保护的
getName () {
return this.name
}
}
const person = new Person()
console.log(person.getName())
// 苹果 -> 一类水果, 这个苹果 -> 实例 香蕉苹果-> 苹果
class Person1 extends Person {
// 可以获得父类的值
constructor() {
super()
console.log(super.getName())
console.log(this.getName())
}
getName () {
return "123"
}
}
const person1 = new Person1()
console.log(person1.getName())
// 类类型接口
interface FoodInterface {
type: string
}
class FoodClass implements FoodInterface {
constructor(public type: string) {}
}
class FoodClass1 implements FoodInterface {
type: string
constructor(arg:string){
this.type = arg
}
}
// 接口继承类
// 1.接口可以继承类,当接口继承了类之后,会继承成员(类型),但不包括实现
// 2.接口还会继承private和protected修饰的成员,但是这个接口只可以被这个类或者它的子类实现
interface I extends Person {}
// 类与类,接口与接口之间使用extends
// 类与接口,implements
class C extends Person implements I{
getName() {
return this.name1 + 'new class C'
}
}
const instance = new C()
console.log(instance.getName())类 要实现 interface 的类型 要用 implements
點擊查看更多內容
為 TA 點贊
評論
評論
共同學習,寫下你的評論
評論加載中...
作者其他優質文章
正在加載中
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦



