Answers for "property does not exist on type object"

2

Property 'of' does not exist on type 'typeof Observable'.

import { of } from 'rxjs';
Posted by: Guest on October-24-2020
2

Property 'on' does not exist on type 'HTMLElement'.

To prevent this error you can write:

var plotDiv: any = document.getElementById('myDiv');
plotDiv.on('plotly_relayout', ...
document.getElementById('myDiv') return HTMLElement. This type doesn't contain method
on because this method is added within plotly-latest.min.js. So in order to silence
the typescript warning you can explicity say compile not to check types for plotDiv

Another way is create type definition like:

interface PlotHTMLElement extends HTMLElement  {
  on(eventName: string, handler: Function): void;
}

var plotDiv  = <PlotHTMLElement>document.getElementById('myDiv')
plotDiv.on('plotly_relayout', function() {

});
Posted by: Guest on January-21-2021
0

Property 'find' does not exist on type NodeListOf

const elements = Array.from(document.querySelectorAll('.selector'));
elements.forEach((...) => {});
Posted by: Guest on June-11-2020
0

property 'do' does not exist on type 'observable<httpevent<any>>'. angular 9

import { tap } from 'rxjs/operators';

const TOKEN_HEADER_KEY = 'Authorization';

@Injectable()
export class Interceptor implements HttpInterceptor {

  constructor(private token: TokenStorage, private router: Router) { }

  intercept(req: HttpRequest<any>, next: HttpHandler):
    Observable<HttpSentEvent | HttpHeaderResponse | HttpProgressEvent | HttpResponse<any> | HttpUserEvent<any>> {
    let authReq = req;
    if (this.token.getToken() != null) {
      authReq = req.clone({ headers: req.headers.set(TOKEN_HEADER_KEY, 'Bearer ' + this.token.getToken())});
    }
    return next.handle(authReq).pipe(tap(
        (err: any) => {
          if (err instanceof HttpErrorResponse) {
            console.log(err);
            console.log('req url :: ' + req.url);
            if (err.status === 401) {
              this.router.navigate(['user']);
            }
          }
        }
      ));
  }

}
Posted by: Guest on October-03-2020
2

Property does not exist on type

// Solution 1: The Quick Fix
// In TypeScript, we can type a function by specifying the parameter types and return types.

// Similarly, we need to type our objects, so that TypeScript knows what is and isn’t allowed for our keys and values.

// Quick and dirty. A quick and dirty way of doing this is to assign the object to type any. This type is generally used for dynamic content of which we may not know the specific type. Essentially, we are opting out of type checking that variable.

let obj: any = {}
obj.key1 = 1;
obj['key2'] = 'dog';

//But then, what’s the point of casting everything to type any just to use it? Doesn’t that defeat the purpose of using TypeScript?

// Well, that’s why there’s the proper fix.

// Solution 2: The Proper Fix
// Consistency is key. In order to stay consistent with the TypeScript standard, we can define an interface that allows keys of type string and values of type any.

interface ExampleObject {
    [key: string]: any
}
let obj: ExampleObject = {};
obj.key1 = 1;
obj['key2'] = 'dog';

// What if this interface is only used once?
// We can make our code a little more concise with the following:

let obj: {[k: string]: any} = {};
obj.key1 = 1;
obj['key2'] = 'dog';
Solution 3: The JavaScript Fix

// Pure JavaScript. What if we don’t want to worry about types?
// Well, don’t use TypeScript ;)

// Or you can use Object.assign().

let obj = {};
Object.assign(obj, {key1: 1});
Object.assign(obj, {key2: 'dog'});
Posted by: Guest on August-09-2021

Code answers related to "property does not exist on type object"

Code answers related to "TypeScript"

Browse Popular Code Answers by Language