Search code examples
javascriptreactjstypescriptdefinitelytyped

Accessing currentTime on SyntheticEvent target


In vanilla react you get currentTime like

(e) => e.target.currentTime

Doing the above in typescript ^3.1.6 results in

Property 'currentTime' does not exist on type 'EventTarget'

SyntheticEvent doesn't seem to list EventTarget anywhere, so I don't know how to get currentTime. The Typescript lib shows it's already defined and my tsconfig (generated via this fork) looks like

"compilerOptions": {
  "lib": ["es6", "dom"]
}

Deps:

"devDependencies": {
  "@types/react-router": "^4.4.0",
  "@types/react-router-dom": "^4.3.1",
  "@types/jest": "^23.3.9",
  "@types/node": "^10.12.2",
  "@types/react": "^16.4.18",
  "@types/react-dom": "^16.0.9",
  "typescript": "^3.1.6"
}

Solution

  • Found the answer here

    What you need to do is to cast the target to the specific element type with the properties you need.

    So to get currentTime you need to do

    (e: React.SyntheticEvent): void => {
      const { currentTime } : { currentTime: number } = e.target as HTMLMediaElement;
    }