Ignore Typescript Errors “property does not exist on value of type”

Ignore Typescript Errors “property does not exist on value of type”

In VS2013 building stops when tsc exits with code 1. This was not the case in VS2012.
How can I run my solution while ignoring the tsc.exe error?
I get many The property ‘x’ does not exist on value of type ‘y’ errors, which I want to ignore when using javascript functions.

Solutions/Answers:

Solution 1:

I know the question is already closed but I’ve found it searching for same TypeScriptException, maybe some one else hit this question searching for this problem.

The problem lays in missing TypeScript typing:

var coordinates = outerElement[0].getBBox();

Throws The property 'getBBox' does not exist on value of type 'HTMLElement'.

The easiest way is to explicitly type variable as any

var outerHtmlElement: any = outerElement[0];
var coordinates = outerHtmlElement.getBBox();

Edit, late 2016

Since TypeScript 1.6 prefered casting operator is as those lines can be squqshed into elegant:

let coordinates = (outerElement[0] as any).getBBox();


Other solutions

Of course if you’d like to do it right, which is an overkill sometimes, you can:

  1. Create own interface which simply extends HTMLElement
  2. Introduce own typing which extends HTMLElement

Solution 2:

The quick and dirty solution is to explicitly cast to any

(y as any).x

The “advantage” is that, the cast being explicit, this will compile even with the noImplicitAny flag set.

Related:  How to reset Jest mock functions calls count before every test

The proper solution is to update the typings definition file.

Please note that, when you cast a variable to any, you opt out of type checking for that variable.


Since I am in disclaimer mode, double casting via any combined with a new interface, can be useful in situations where you

  • do not want to update a broken typings file
  • are monkey patching

yet, you still want some form of typing.

Say you want to patch the definition of an instance of y of type OrginalDef with a new property x of type number:

const y: OriginalDef = ...

interface DefWithNewProperties extends OriginalDef {
    x: number
}

const patched = y as any as DefWithNewProperties

patched.x = ....   //will compile

Solution 3:

You can also use the following trick:

y.x = "some custom property"//gives typescript error

y["x"] = "some custom property"//no errors

Note, that to access x and dont get a typescript error again you need to write it like that y["x"], not y.x. So from this perspective the other options are better.

Related:  Return positions of a regex match() in Javascript?

Solution 4:

There are several ways to handle this problem. If this object is related to some external library, the best solution would be to find the actual definitions file (great repository here) for that library and reference it, e.g.:

/// <reference path="/path/to/jquery.d.ts" >

Of course, this doesn’t apply in many cases.

If you want to ‘override’ the type system, try the following:

declare var y;

This will let you make any calls you want on var y.

Solution 5:

When TypeScript thinks that property “x” does not exist on “y”, then you can always cast “y” into “any”, which will allow you to call anything (like “x”) on “y”.

Theory

(<any>y).x;

Real World Example

I was getting the error “TS2339: Property ‘name’ does not exist on type ‘Function'” for this code:

let name: string = this.constructor.name;

So I fixed it with:

let name: string = (<any>this).constructor.name;

Solution 6:

Had a problem in Angular2, I was using the local storage to save something and it would not let me.

Related:  One time page refresh after first page load

Solutions:

I had localStorage.city -> error -> Property 'city' does not exist on type 'Storage'.

How to fix it:

localStorage[‘city’]

(localStorage).city

(localStorage as any).city