首页 > 其他分享 >typescript装饰器

typescript装饰器

时间:2022-11-11 18:57:59浏览次数:42  
标签:function typescript return string logicType console 装饰 metadata

属性装饰器

  1. 参数
export default function (proto, key) {
  // 两个参数
}
  1. 给属性增加metadata
import 'reflect-metadata';
export default function (label, type?) {
  // 在装饰器内,增加元数据,能够自动加到属性上
  return Reflect.metadata("key",  value);
};

// 读取metadata
Reflect.getMetadata("key", obj, name)

访问器装饰器

  1. 能够间接的给属性增加转换器
import 'reflect-metadata';
export default function (logicTypeName: string) {
  console.log(arguments);
  return function (target: any, key: string, descriptor: PropertyDescriptor) {
    descriptor.get = function () {
      return this[logicTypeName] + "___";
    };
  };
}

export class FlowParamDesc {
  @ReaderField("逻辑类型")
  logicType?: string;
  @ReaderField("逻辑值")
  logicValue?: string;
  
  @LogicTypeGetter("logicType")
  get getLogicType() {
    return "";
  }
}

const f = new FlowParamDesc();
  f.logicType = "123";
  console.log(f);  // {"logicType":"123"}
  console.log(f.getLogicType);  // 123___
  console.log(JSON.stringify(f));  // {"logicType":"123"}

方法装饰器

标签:function,typescript,return,string,logicType,console,装饰,metadata
From: https://www.cnblogs.com/zhuxiang1633/p/16881449.html

相关文章

  • [Typescript] 97. Hard - Capitalize Words
    Implement CapitalizeWords<T> whichconvertsthefirstletterof eachwordofastring touppercaseandleavestherestas-is.Forexampletypecapitalized......
  • python基础(三)装饰器
    字典推导式:data_list=['1hello','2world']result={item.split("")[0]:item.split("")[1]foritemindata_list}print(result)data='wd=搜狗&rsv_spt=1&r......
  • 安装 TypeScript 并编译成JS
    官网:https://github.com/microsoft/TypeScriptTypeScript是一种由微软开发的开源、跨平台的编程语言。它是JavaScript的超集,最终会被编译为JavaScript代码。TypeScript......
  • OJ中Typescript语法整理
    基础原始类型原始类型:number/string/boolean/null/undefined/symbol对象类型:oject(数组,对象,函数等)自定义复杂的对象类型:typeCustomArray=(number|string)let......
  • [Typescript] 95. Hard - Required Keys
    Implementtheadvancedutiltype RequiredKeys<T>,whichpicksalltherequiredkeysintoaunion.ForexampletypeResult=RequiredKeys<{foo:number;bar?:......
  • [Typescript] 96. Hard - Optional Keys
    Implementtheadvancedutiltype OptionalKeys<T>,whichpicksalltheoptionalkeysintoaunion. /*_____________YourCodeHere_____________*/typeOp......
  • typescript 泛型
    一、泛型与any类型的区别泛型是等待确定的占位类型,可以理解为函数的形参;所以泛型是固定的某一个类型,实例化的时候确定其实际类型any类型是顶级类型,它包括了所有的基......
  • [Typescript] 93. Hard - Get Required
    Implementtheadvancedutiltype GetRequired<T>,whichremainsalltherequiredfieldsForexampletypeI=GetRequired<{foo:number,bar?:string}>//exp......
  • [Typescript] 94. Hard - Get Optional
    Implementtheadvancedutiltype GetOptional<T>,whichremainsalltheoptionalfieldsForexampletypeI=GetOptional<{foo:number,bar?:string}>//expe......
  • TypeScript(基础篇)day01
    一.TS介绍1.1简介ts是2012年由微软开发,在js的基础上添加了类型支持1.2优劣势优势:任何位置都有代码提示,增加效率;类型系统重构更容易;使用最新的ECMAscript语法劣势:和......