Closed squarewave24 closed 8 years ago
It's because nativeElement
does not exposes any type information (https://angular.io/docs/js/latest/api/core/index/ElementRef-class.html#!#nativeElement-anchor), leaves consumer to correctly infers type for each DOM element as necessarily.
You may able to cast nativeElement to corresponding eventemitter type (https://github.com/ReactiveX/rxjs/blob/c76a14d4e236139076db94086648bdbce88b2e15/src/observable/FromEventObservable.ts#L7-L35), or cast event to any
to access its properties without type information.
thanks! i tried casting it like this, but it's not helping.
var keyups = Observable.fromEvent(this.input.nativeElement as any, 'keyup')
.map( e => {
return e.target.value; // <-- target does not exist on {}
})
this.input.nativeElement as any
isn't typescript syntax to cast.
https://www.typescriptlang.org/docs/handbook/basic-types.html
maybe it's a newer feature ? https://basarat.gitbooks.io/typescript/content/docs/types/type-assertion.html
any way you can give me an example of how to do it in my situation? i tried casting the argument parameter as well
Main reason in here is nativeElement
is already none-type inferrable, as nativeElement: any
. So casting it again to <any>nativeElement
is redundant.
As commented above, you could go with 2 options (https://github.com/ReactiveX/rxjs/issues/1822#issuecomment-231903246)
nativeElement
to corresponding eventEmitter type based on element e
to corresponding event typeor if would like to shortest workaround to make compiler don't worry about types, you may also able to try
map((e: any) => {...})
to bypass compiler.
As I believe this isn't erroneous behavior of current type definition of operators, let me close this issue. If you think there is incorrect behavior in library, please feel freely reopen / or file new issue with minimal codebases can reproduce issue (via small jsfiddle, or github repo, etcs)
thanks for responding so quickly! As you likely already guessed I am new to Typescript :)
thanks again
I tried a few approaches and finally got rid of the nativeElement as a test, and the same error persists. this leads me to believe it was not the nativeElement
all along.
var $input = $('#Search');
var keyups = Observable.fromEvent($input, 'keyup')
.map( e => {
return e.target.value; // [ts] Property 'target' does not exist on type '{}'.
})
Better late than never, I believe the syntax you're looking for is Observable.fromEvent<KeyboardEvent>($input, 'keyup')
.
maybe you can try using this: e['target'].value, that works for me.
TS7017: Element implicitly has an 'any' type because type '{}' has no index signature. @jochenshi I got this with e['target']
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
following the Angular2 QuickStart setup, I've added following code:
my code:
compilation error happens here. Not sure how to troubleshoot this:
I have a feeling it's just a matter of telling TypeScript where to find the definition for the event, but all the examples i've found so are pretty much do the same imports
package.json