Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 657
daniel Avatar asked Aug 06 '13 14:08

daniel


People also ask

How do you ignore property does not exist on type?

Use a type assertion to ignore the 'Property does not exist on type' error in TypeScript, e.g. (obj as any). myProperty . Casting the object to any disables type checking and allows us to access any property on the object without getting any errors.

How do I ignore TypeScript error?

Use // @ts-ignore to ignore the type checking errors on the next line in a TypeScript file. If you use a linter, you might have to add a comment to also suppress linting errors when using ts-ignore - // eslint-disable-next-line @typescript-eslint/ban-ts-comment .

Does not exist on type window TypeScript?

The "Property does not exist on type 'Window & typeof globalThis'" error occurs when we access a property that does not exist in the Window interface. To solve the error, extend the Window interface in a . d. ts file adding the property you intend to access on the window object.

Does not exist on type string?

The "Property does not exist on type String" error occurs when we try to access a property that does not exist on the string type. To solve the error, use an object instead of a string, or make sure you're accessing a valid built-in method on the string.


2 Answers

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
like image 132
michalczukm Avatar answered Sep 21 '22 17:09

michalczukm


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:

const y: OriginalDef = ...  interface DefWithNewProperties extends OriginalDef {     x: number }  const patched = y as any as DefWithNewProperties  patched.x = ....   //will compile 
like image 43
Bruno Grieder Avatar answered Sep 20 '22 17:09

Bruno Grieder