I'm using VS2019 and TypeScript 4.3.5. The tsconfig.json file:
"compilerOptions": {
"lib": [ "ES2017.String", "DOM", "ES2020.Intl", "ES2017" ],
"target": "ES2017"
}
points to "DOM", i.e. lib.dom.d.ts declarations file containing the touch event declarations. Here are two (of more):
interface Touch {
readonly altitudeAngle: number;
readonly azimuthAngle: number;
readonly clientX: number;
readonly clientY: number;
readonly force: number;
readonly identifier: number;
readonly pageX: number;
readonly pageY: number;
readonly radiusX: number;
readonly radiusY: number;
readonly rotationAngle: number;
readonly screenX: number;
readonly screenY: number;
readonly target: EventTarget;
readonly touchType: TouchType;
}
interface TouchEventInit extends EventModifierInit {
changedTouches?: Touch[];
targetTouches?: Touch[];
touches?: Touch[];
}
Yet, when I write the following line:
const event: Event = window.event;
for (var i = 0; i < event.touches.length; i++);
I get the error: TS2339(TS) Property 'touches' does not exist on type 'Event'.
I'm fairly new to typescript, any ideas what I'm doing wrong?
Event is the root type of all events in window, other events need to inherit that event
So there will be no touches property inside Event itself, you will need to find other extended types that has touches property such as TouchEvent
Also, event is a preserve name for the native window.event, try to use another name for your type.
Then your code should be like:
const customEvent: TouchEvent = window.event;
for (var i = 0; i < customEvent.touches.length; i++);