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