Typescript ES7 descriptors with ES3 output?

After looking at the problems, it seems that TS should support ES3 with decorators, and I have a script in which I have an existing typescript code base that uses decorators and target ES5, but now I need to explicitly support IE6, which requires ES3.

Now according to: https://github.com/Microsoft/TypeScript/issues/4681

It seems that ES3 should be supported, but if I output to the target ES3, I get:

error TS1241: Unable to resolve signature of method decorator when called as an expression. Supplied parameters do not match any signature of call target.

I get 0 errors and everything works in ES5, so you need to do something to make it work in ES3 or just not supported?

Here is a cloud example:

https://ide.c9.io/grofit/knockout-decorators-es3-example

just run gulp on the command line, if you change the tsconfig target to es5, it will work.

+6
source share
1 answer

It seems that when you target ES3, method decorators are not supported properly or not supported at all. Unfortunately, the error message you get is not very useful. There seems to be a discussion in the error message. In addition, it’s not clear to me whether they plan partial support for decorators targeting ES3 or full support.

For example, if you try to use an ES3-oriented method decorator:

 function myMethodDecorator(target: Object, propertyKey: string, descriptor: TypedPropertyDescriptor<any>): TypedPropertyDescriptor<any> { // do something return descriptor; }; class MyClass { @myMethodDecorator myMethod(arg: string) { return "Message -- " + arg; } } 

You will receive the error message that you reported:

error TS1241: Unable to resolve method decorator signature when invoked as expression. The supplied parameters do not match the signature of the target call.

But if you try to apply a property descriptor even though you apply it to a method, the compiler is strangely alright with that. This compiles ES3 targeting without errors:

 function myPropertyDecorator(target: Object, propertyKey: string): void { // something }; class MyClass { @myPropertyDecorator myMethod(arg: string) { return "Message -- " + arg; } } 

However, you can force it to compile ES3 when using method decorators:

 let myMethodDecorator: any = function(target: Object, propertyKey: string, descriptor: TypedPropertyDescriptor<any>): TypedPropertyDescriptor<any> { // do something return descriptor; }; class MyClass { @myMethodDecorator myMethod(arg: string) { return "Message -- " + arg; } } 
+7
source

All Articles