ts学习笔记

如何学习一门语言

是什么? 与es的关系?

图片描述
以面向对象的思维编程?

代码格式的流转?

图片描述

我们在做什么?

图片描述
图片描述

// C-D 强数据操作
import { observable, action } from 'mobx'

export default class StoreBase {
  service: any

  @observable state = ''
  @observable list = []
  @observable loading = false
  @observable totalCount = 0
  @observable counter = 0
  @observable pageSize = 10
  @observable pageIndex = 1
  @observable submiting = false
  @observable initialFormValue = {}

  constructor(service) {
    this.service = service
  }

  @action changeSize(pageSize) {
    this.pageSize = pageSize
  }
  @action async getPageList(options: { pageIndex?: number, pageSize?: number }) {
    options.pageIndex = options.pageIndex || this.pageIndex
    options.pageSize = options.pageSize || this.pageSize
    this.pageIndex = options.pageIndex
    this.pageSize = options.pageSize
    this.loading = true
    const result = await this.service.list(options)
    this.loading = false
    this.list = result.data
    this.totalCount = result.totalCount
  }
  @action async add(body) {
    this.submiting = true
    try {
      const result = await this.service.add(body)
      if (result.statusCode && result.statusCode !== 200) {
        throw new Error(result.message)
      }
    } catch (error) {
      throw error
    } finally {
      this.submiting = false
    }
  }
  @action async edit(id, body) {
    this.submiting = true
    try {
      const result = await this.service.update(id, body)
      if (result.statusCode && result.statusCode !== 200) {
        throw new Error(result.message)
      }
    } catch (error) {
      throw error
    } finally {
      this.submiting = false
    }
  }

  @action async remove(id) {
    try {
      await this.service.delete(id)
      this.getPageList({ pageIndex: this.pageIndex })
    } catch (error) {
      throw error
    }
  }

  @action initializeForm(form) {
    this.initialFormValue = form
  }
}

图片描述
图片描述
图片描述

我们做的web application是由组件集组装起来的, application的质量取决于 组件的质量
什么是组件的质量? 怎样写出质量好的组件?

ts 有什么? 是如何使得我们能产出高质量组件

类型系统 之 原子类型

图片描述

// boolean
let judge: boolean = true;

// string
let girl: string = `${family.join('======')}`;

// any
const think: any = {}
think = [];
think = '';
think = 1037;

// void
function select(): void { 
    return null;
}

// never
function logger(message: string): never { 
    throw new Error(message);
}
类型系统 之 组合类型

图片描述

// 数组类型
let family: string[] = ["epuisite", "love", "detail"];
let team: Array<number> = [1, 3, 7];
interface teamArray { 
    [index: number]: any
}
let t: teamArray = [1, 2, 3, {1: 3}];
// 元组
let structure: [string, Array<string>];
structure = ['why', ['atomic element', 'what']];
structure[2] = 'how';
structure[3] = ['when'];
// function类型
interface Doing { 
    (type: number, thing: string, when?: string|number): any[]
}

let mydoing: Doing;
function f(type: number = 2, thing?: string, when?: string|number, ...items: any[]) { 
    return type;
}

function sf(message: number): number;
function sf(message: string): string;
function sf(message: number | string): number | string { 
    if (message as number) {
        return 1;
    } else { 
        return '123';
    }
}
// 枚举类型  数字类型的值
const Color = {Red, Green, Blue};
enum Direction{  
    Up=1,  
    Down,  
    Left,  
    Right  
}  
// 自定义类型
// 对象类型 - 接口
interface Mother { 
    readonly id: number,
    detail: any[],
    learning: string,
    love: boolean,
    housework?: boolean,
    [proName: string]: any
}
let me: Mother = {
    id: new Date().getTime(),
    detail: ['water', 137, 'cloud', 'grass', 'nose'],
    learning: 'a wider world',
    love: true,
    doing() { 
        return 137;
    }
};

图片描述
图片描述

工具 是什么? 怎么用? 为什么用?

图片描述

泛型

图片描述

function createArray<T>(length: number, value: T): Array<T> { 
    let result: T[] = [];
    for (let i = 0; i < length; i++) { 
        result[i] = value;
    }
    return result;
}

function swap<T = number, U = number>(tuple: [T, U]): [U, T] { 
    return [tuple[1], tuple[0]];
}


interface lengthwish { 
    length: number
}

function swap2<T extends lengthwish, U>(tuple: [T, U]): [U, T] { 
    return [tuple[1], tuple[0]];
}

interface createArrayFunc { 
    <T extends lengthwish>(length: number, value: T): Array<T>;
}

let myCreateArray: createArrayFunc;
myCreateArray = function <T extends lengthwish>(length: number, value: T): Array[T] { 
    let result: T[] = [];
    for (let i = 0; i < length; i++) { 
        result[i] = value;
    }

    return result;
}


// 联合类型[共有的属性和方法 + 类型推论]
let cooperation: string | number | boolean | null | undefined;
cooperation = 1;
cooperation = null;



// 类型断言
function determine(config: number[] | string ): boolean {
    if ((<string>config).length || (<number[]>config).length) {
        return true;
    } else { 
        return false
    }
 }

type Name = string;
type DoFn = () => string;
type cof = Name | Dofn;
function o(message: cof): Name { 
    return '';
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值