326

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.

10 Answers 10

417

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, the prefered casting operator is as, so those lines can be squashed into:

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
Sign up to request clarification or add additional context in comments.

11 Comments

You could also create an interface that extends HTMLElement and has the additional getBBox property. That way you still get code completion on the other properties.
instead of casting into any to getBBox is there any method to cast propely ? like to find out the type of getBBox ?
nice! var coordinates = (<any>outerElement[0]).getBBox();
This actually does not answer the question: "How to IGNORE the errors"
Explicitly casting it to type any (myVariable as any) solved it. Thanks.
|
204

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.

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
  • and/or you 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:

// The `y` variable has a typing `OriginalDef` missing a property `x`
const y: OriginalDef = ...

// create an interface with your new property(ies)
interface DefWithNewProperties extends OriginalDef {
    x: number
}

// patch the current `y` to the new interface using double casting
const patched = y as any as DefWithNewProperties

// the patched value can now access the new property
// the type inference and type checker will work correctly
patched.x = ....   //will compile

2 Comments

thanks this helped: import http = require('http'); var server = http as any; server.Server(app); //ignores ts errors!
I used this in "ensure property not found on NodeRequire". so I declared my require variable on NodeRequired and (require as any).ensure for the property. Hope this helps.
87

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.

4 Comments

Does anyone know why this works, and if this has any potential ramifications or benefits over initially declaring the object as :any?
It would have the clear benefit of keeping the typings, rather than casting to any. I'd like to know why this doesn't throw a warning but accessing directly does..
seriously !?! this is so far beyond hackish... Yes it works because you are looking for the custom property using a string, but please don't. Renaming the property won't work with an IDE refactoring and it is mega error prone to use a hardcoded string.
@gotjosh suggest the alternative please.
44

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.

5 Comments

Should be /// <reference path="/path/to/jquery.d.ts" /> with the self closing tag at the end
I am using VS2015 and followed this tutorial for angular i don't have jquery.d.ts file in my project
@Dimple npm install -g tsd then tsd install jquery
The second option (declare var y) works great if you are migrating from JavaScript to TypeScript, and want to avoi error TS2304 because your old JavaScript is referencing a variable in another JavaScript file.
Thanks! For me the issue was with Jest, const mockPrompt: any = jest.spyOn(step, 'prompt');
31

I know it's now 2020, but I couldn't see an answer that satisfied the "ignore" part of the question. Turns out, you can tell TSLint to do just that using a directive;

// @ts-ignore
this.x = this.x.filter(x => x.someProp !== false);

Normally this would throw an error, stating that 'someProp does not exist on type'. With the comment, that error goes away.

This will stop any errors being thrown when compiling and should also stop your IDE complaining at you.

3 Comments

Is there a tsconfig compilerOptions flag to completely disable this?
Not that I'm aware of. However, you can do it per file by placing @ts-nocheck at the top of each file.
yeah, need better solution for that
28

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;

1 Comment

Doesn't work with super. If you extend a class using typings and author forgets a public method you're pretty much screwed. You have to add it to the type definition, which gets stopped on the next npm install, forcing you to create a pull request or otherwise notify the author, which is probably a good thing but a pain.
19

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

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

2 Comments

Second option looks cool, but doesn't seem to do the job anymore. Works if you prefix an object with <any> - (<any>localStorage).city.
I know this is old but your top example just worked for me.. Well done.
8

A quick fix where nothing else works:

const a.b = 5 // error

const a['b'] = 5 // error if ts-lint rule no-string-literal is enabled

const B = 'b'
const a[B] = 5 // always works

Not good practice but provides a solution without needing to turn off no-string-literal

2 Comments

I do this too but some platforms (like Google Cloud) will raise a warning message suggesting that a.b is better than a['b']. Do you know why this is?
Not sure but you can, in tslint.json for example, change the options so that it prefers a.b
1

In my particular project I couldn't get it to work, and used declare var $;. Not a clean/recommended solution, it doesnt recognise the JQuery variables, but I had no errors after using that (and had to for my automatic builds to succeed).

Comments

1

I was able to get past this in typescript using something like:

let x = [ //data inside array ];
let y = new Map<any, any>();
for (var i=0; i<x.length; i++) {
    y.set(x[i], //value for this key here);
}

This seemed to be the only way that I could use the values inside X as keys for the map Y and compile.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.