首页 > 其他分享 >typescript Decorators TypeError: Function expected

typescript Decorators TypeError: Function expected

时间:2024-03-13 20:12:40浏览次数:14  
标签:Function typescript return -- PropertyDescriptor id expected 装饰

需要启用experimentalDecorators

 

要启用对装饰器的实验性支持,您必须在命令行或tsconfig.json中启用ExperimentalDecorators编译器选项:

command Line:

tsc --target ES5 --experimentalDecorators   这个文档说的很有问题,说是要“实验性支持”,而实际上,自ts5之后,启用这个选项实际上是“使用old style decorators/old legacy decorators”  
TypeScript 5.0 添加了对 ECMAScript 装饰器的支持。它从很久以前(大约 7 年)就支持所谓的 TypeScript 装饰器。这两种类型的装饰器不兼容。编译器选项--experimentalDecorators触发旧式装饰器的使用,它们通常需要--emitDecoratorMetadata才能正常工作。首先,您必须决定要使用哪种类型的装饰器。
    // 在函数调用前执行格式化操作 export const parseDecorator = (     target: any,     propertyName: string,     descriptor: PropertyDescriptor, ): PropertyDescriptor => {     return {         ...descriptor,         value(...args: any[]) {            ......         },     }; }; // 这里,这个方法装饰器会返回一个对象(PropertyDescriptor)而实际需要一个Function,所以就报错了
export interface UserType {     id: number;     username: string; }
class UserService {     private users: UserType[] = [         { id: 1, username: 'admin' },         { id: 2, username: 'pincman' },     ];
    @parseDecorator     delete(id: number) {         ......         return this;     } }    

编译出来

 

var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
    function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; } // 这个f就是返回的PropertyDescriptor对象,可以看到typeof不能是对象,需要是函数

......

        else if (_ = accept(result)) {

标签:Function,typescript,return,--,PropertyDescriptor,id,expected,装饰
From: https://www.cnblogs.com/hhdom/p/18071418

相关文章

  • [计算理论] 1. 图灵机、递归函数与丘奇-图灵论题 Turing Machine, Recursive Function
    图灵机在研究一种自动机时,我们有两种视角语法学(Syntax),描述一个自动机是什么,如分析自动机的组成、结构。语义学(Semantics),描述一个自动机做什么,如分析自动机的语言。换句话说,前者是自动机的视角,后者是形式语言的视角。图灵机的语法图灵机的原始描述如下:一台含......
  • [js error] SyntaxError: Unexpected token ‘{‘ (at uniFile.js?t=1710138723630:1:
    问题详情问题描述封装一个函数的时候报错问题原因SyntaxError:Unexpectedtoken‘{’(atuniFile.js?t=1710138723630:1:34)SyntaxError:意外的令牌“{”(在uniFile.js?t=1710138723630:1:34)意思是有不符合语法规范的地方在第一行34个字符的地方去到报错文件的地方查......
  • [转]Golang Functional Options Pattern
     原文: https://golang.cafe/blog/golang-functional-options-pattern.html-------------------- GolangFunctionalOptionsPatternTheGo(Golang)FunctionaOptionsPatternisaway,apatternofstructuringyourstructsinGobydesigningaveryexpressivea......
  • 创建Vue3+Vite+TypeScript项目
    一、安装node环境,安装18.0或更高版本的Node.js  推荐使用nvm管理node版本:一看就会使用nvm实现多个版本的node自由切换-始是逍遥人-博客园(cnblogs.com)二、创建项目  1、选择一个工作路径,如:E:\webproject  2、打开cmd命令窗口进入到当前目录    快捷方式:直接......
  • Typescript学习笔记(一)
    学习日期:03-09-2024关键字:Typescript;安装;原始数据类型;Any类型;数组;元组;Typescript是Javascript的超集,显著区别是加了静态类型风格的类型系统、es6-es10-esnext的语法支持安装npminstall-gtypescript原始数据类型Boolean、Null、Undefined、Number、BigInt、String、Sy......
  • 4-3nn.functional和nn.Module
    importtorchimporttorchkerasprint("torch.__version__="+torch.__version__)print("torchkeras.__version__="+torchkeras.__version__)"""torch.__version__=2.1.1+cu118torchkeras.__version__=3.9.4"""1......
  • 关于Flask中View function mapping is overwriting an existing endpoint function
    关于Flask中Viewfunctionmappingisoverwritinganexistingendpointfunction首次编辑:24/3/10/11:03最后编辑:24/3/10/11:57引子背景本来是在写个人网站,以前的代码中,几乎每个视图函数都有类似于:@app.route("/")defindex(): try: returnsend_file("index.html") e......
  • 2024-03-05 NestJs学习日志之新建nest项目,运行启动命令nest start报错:Could not find
    如题,低级错误。具体报错:CouldnotfindTypeScriptconfigurationfile"tsconfig.json".Please,ensurethatyouarerunningthiscommandintheappropriatedirectory(insideNestworkspace)找不到TypeScript配置文件“tsconfig.json”。请确保您在适当的目录(Nest工作......
  • JSON.parse解析字符串报错-SyntaxError: Unexpected token ‘ in JSON at position 报
    “SyntaxError:Unexpectedtoken’inJSONatposition”报错原因是因为解析的字符串对象中,JSON.parse无法识别;JSON.parse可以将标准的json类型数据转换为JavaScript对象,如果数据不是正确的json类型的数据则会控制台报错,可能会阻断代码的正常运行我们可以写一个函数来......
  • lua5.1 - function env
    注意:只在lua5.1才支持,后面的lua版本做了改动不再兼容 myEnv.lualocalmyEnv={}myEnv.a=1myEnv.b="one"myEnv.log=printreturnmyEnv Test.lualocalmyEnv=require("myEnv")setfenv(1,myEnv)--调用上面的函数后,print将没法使用因为那是在全局env......