为了账号安全,请及时绑定邮箱和手机立即绑定

【备战春招】第12天 nest js ts

标签:
Node.js

课程名称: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}`
}

https://img1.sycdn.imooc.com//63f1d62900016a0f09320359.jpg




https://img1.sycdn.imooc.com//63f1d63200019df507330487.jpg

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

https://img1.sycdn.imooc.com//63f1d6530001ec4d04480308.jpg

https://img1.sycdn.imooc.com//63f1d66a0001b00a11320621.jpg


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
Web前端工程师
手记
粉丝
25
获赞与收藏
19

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消