-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Syntax Error when Decorator mutates original Entity #7687
Comments
So the decorator will pass in |
Sorry, if my example was confusing, I guess it needs some understanding of Angular, which shouldn't be necessary. I try again :) So, applying the SomeComponentController.$inject = ['SomeService]; This attached meta information is only relevant to Angular's Dependency Injection. The angular
.module('app')
.service('SomeService', SomeService);
class SomeService {} The second decorator class Component {
public transclude = true;
public template = '<span>foo</span>';
public bindings = { input: '>' };
public require = null;
public controller = SomeComponentController;
} Obviously this construct doesn't really exist, only through the close created by applying the decorator. But I wanted to show what will get passed to the So really, when writing this: import * as angular from 'angular';
import SomeComponent from './some.component';
export default angular
.module('app', [])
.component('someSome', new SomeComponent()); // => new Component()
But I do not need (and also can not create the tl;dr I am mutation a class via a decorator. The original class does required some arguments for instantiation, but the class originated from the decorator doesn't. The TS compiler does not recognize this at development time and displays an error. Compiling the code does work though. |
The problem is that the type mutation in the decorator is not captured. the assumption is you will return something of the same shape. issue #4881 tracks this work. for now, as a work around, you can make the constructor argument optional, this will make sure |
Feared I have to do that. Thanks for the help. Really appreciate it! :) |
I wrote decorators (for Angular 1.x) to have some cleaner syntax when defining a
component
and using the DI.The
@Component
decorator (see below) will mutate the original class in such a way that it will conform theng.IComponentOptions
type (when created). The original class will be "attached" to thecontroller
attribute, etc.So the decorator basically wraps the
class
inside anotherclass
from typeComponent
. The resultingComponent
can be used to register itself with Angular (see below). The other decorator called@Inject
will add meta data ($inject
property) to the original class, so the DI will still work when the code is minified.In the below example I am importing and creating a
SomeComponentController
, which is decorated with@Component
and@Inject
. The TS compiler will show a syntax error, because the imported component is not called with any arguments. But since theComponent
type doesn't require any, it is fine. Angular will inject the correct dependencies when it instantiates the controller (SomeComponentController
).It looks like a hard problem for the compiler to infer all this information, but is there a way to let it know that everything is fine? :) Because compiling and running the code works finde. Only the editor is not satisfied with the code.
TypeScript Version:
1.8.9
Code
Expected behavior:
No error. Compiler recognizes the mutation.
Actual behavior:
Editor (vscode) shows "TS2346: Supplied parameters do not match any signature of call target." error.
But compiling the code actually still works without any errors (via webpack).
The text was updated successfully, but these errors were encountered: