Error: The 'currentTime' property is not found on the 'EventTarget & HTMLInputElement' type

In my current nextJS project, I am utilizing TypeScript and incorporating a cdn version of flowplayer asynchronously. This player extends the event.target with new attributes.

However, during the build process, I encountered this error message: Type error: Property 'currentTime' does not exist on type 'EventTarget & HTMLInputElement'.

To resolve this, I need to intersect it with specific attributes: currentTime, duration, opts. My attempted solution is as follows:

type FlowPlayerEvent<T extends EventTarget> = Event & {
target: T;
currentTime: Number;
duration: Number;
opts: Object;
};

This is the event handler in question:

function stateHandler(ev : Event) {
const Event: FlowPlayerEvent = ev.target;
if (ev.type === 'pause') { console.log('paused'); }
if (ev.type === 'playing') { console.log(`Playing at ${Event.currentTime}, duration is: ${Event.duration}, video ID is: ${Event.opts.metadata.id}`); }
if (ev.type === 'timeupdate') { console.log(Event.currentTime); }
if (ev.type === 'ended') { console.log('The end'); }

}

Upon hovering over FlowPlayerEvent, I receive the following message: Generic type 'FlowPlayerEvent' requires 1 type argument(s).ts(2314)

Could someone advise on the correct way to extend it in this instance?

Appreciate any help you can provide!

Answer №1

There may be some confusion between the event and the event target. Remember, the currentTime and duration properties belong to the target element, not the event itself. These properties are specific to the native HTMLVideoElement. The addition of the opts property by flowplayer can make typing a bit cumbersome.

Without prior knowledge of flowplayer, it's necessary to consult the documentation. It remains unclear if there are existing typescript types for this package. For your immediate use case, consider implementing the following:

type FlowPlayerElement = HTMLVideoElement & {
    opts: {
        metadata: {
            id: string;
        }
    }
}

type FlowPlayerEvent = Event & {
    target: FlowPlayerElement;
};
function stateHandler(ev: FlowPlayerEvent) {
    const target = ev.target;
    if (ev.type === 'pause') { console.log('Paused'); }
    if (ev.type === 'playing') { console.log(`Playing at ${target.currentTime} with duration: ${target.duration} and video ID: ${target.opts.metadata.id}`); }
    if (ev.type === 'timeupdate') { console.log(target.currentTime); }
    if (ev.type === 'ended') { console.log('End of video'); }
}

Answer №2

How should it be properly extended in this scenario?

const Event: FlowPlayerEvent<MyType> = ev.target;

In order to extend it correctly, a type argument must be passed.

Answer №3

give this a shot

function handleVideoStateChange(ev: React.ChangeEvent<HTMLVideoElement>) {
    const video = ev.target;
    if (ev.type === 'pause') { console.log('video paused'); }
    if (ev.type === 'playing') { console.log(`Playing at ${video.currentTime}, duration is: ${video.duration}, and the video id is: ${video.opts.metadata.id}`); }
    if (ev.type === 'timeupdate') { console.log(video.currentTime); }
    if (ev.type === 'ended') { console.log('End of video'); }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using ChartJS in React to style points as images

To create plots in React/NextJS, I am utilizing chartjs and react-chartjs-s. For a specific plot, I want to use an image as the pointStyle. In regular JavaScript, an image can be generated with const i = new Image() and then employed as the pointStyle in C ...

Taking advantage of Input decorator to access several properties in Angular 2

I am currently working on a component that is designed to receive two inputs through its selector. However, I would like to make it flexible enough to accept any number of inputs from various components. Initially, I tried using a single @Input() decorator ...

Developing OnIdle with rxjs

As I explore rxJS, I've come across this code snippet that monitors browser activity such as mouse movements, clicks, and keyboard usage. It's like the opposite of onIdle. import { fromEvent, throttle, debounce, interval, merge } from 'rxjs& ...

Selecting ion-tabs causes the margin-top of scroll-content to be destroyed

Check out the Stackblitz Demo I'm encountering a major issue with the Navigation of Tabs. On my main page (without Tabs), there are simple buttons that pass different navparams to pre-select a specific tab. If you take a look at the demo and click t ...

Using TypeScript with MongoDB aggregation

Recently, I've been working on a User model with the goal of achieving generic aggregation. Essentially, I want to be able to pass an array of objects to a function and have it execute smoothly. Let me provide you with a snippet of the getUser functi ...

What is the best way to store image assets for faster loading times on NextJS?

The documentation for NextJS explicitly mentions that you cannot define Cache-Control headers in the next.config.js file, as they will be replaced in production to optimize caching of API Routes and static assets. Is there another method to cache image as ...

What triggers the useEffect to execute when utilized alongside useContext?

I have a situation where I call useEffect inside useContext and I am interested in knowing when this particular useEffect is triggered. [settingContext.tsx] // defining the ColorContext object export const ColorContext = createContext<ColorContextType& ...

Oops! An issue occurred on the server: ReferenceError - 'window' is not defined. This error occurred during

I'm looking to integrate a Microsoft Login Button into a Next Js application using Material UI. However, after adding the necessary package from NPM and refreshing the page, I encountered the error mentioned above. When I tried setting ClientId="a973 ...

The limitations of Typescript types influence the program's behavior

As a newcomer to the Typescript environment, I am currently developing a test application to familiarize myself with it. However, I have encountered an issue regarding type restrictions that seems to be not working as expected. In my class, I have defined ...

Contrasting the redirect object with res.redirect in Next.js

Can you explain the contrasting features in Next.js between return { redirect: { permanent: false, destination: '' } }; And res.redirect() ...

What is the process for retrieving information from a JSON document?

When working on my project using Next.js, I encountered an issue with fetching and displaying data from a JSON file named mainMenu. Could someone please guide me on how to correctly implement this? I have shared my code below: function MyApp({ Component, ...

Using React with Typescript: How to pass a function as a prop to a child component and call it from within

I am trying to pass a function as a prop to a child component so that the child can call it. Here is my parent component: interface DateValue { dateValue: string; } const Page: React.FC = () => { const dateChanged = (value: DateValue) => { ...

Storing and retrieving user credentials across different applications using NativeScript and Angular 2

I am looking to store user credentials (username and password) in a Nativescript application. In another Nativescript application, I need to be able to retrieve these stored credentials. Unfortunately, I am unsure of how to accomplish this in Nativescript ...

What is the best way to implement asynchronous guarding for users?

Seeking assistance with implementing async route guard. I have a service that handles user authentication: @Injectable() export class GlobalVarsService { private isAgreeOk = new BehaviorSubject(false); constructor() { }; getAgreeState(): Obser ...

What is the method for locating an element within an array?

The content being returned is presenting a challenge. How can I retrieve data from inside 0? I attempted to access it using date[0] without success const { data } = getData(); The result of console.log(data) is shown below: enter image description here ...

Automatically collapse the Shadcn-UI Accordion when the mouse exits

For my self-education project, I am working on building a sidebar with an accordion using Shadcn-ui, NextJS (13.4), React, and Node. Being new to these technologies, I have encountered a problem that I can't seem to solve. The sidebar expands on mous ...

The issue of Next.js redux useSelector causing HTML inconsistency

Currently, I am utilizing Next.js for the development of a React application. In order to manage the state, I have integrated redux along with redux-toolkit. A peculiar error has surfaced in the console with the message: Warning: Did not expect server H ...

How to retrieve text from ion-textarea in Ionic 3 upon clicking a button

One of my tasks involves utilizing an ion-textarea for users to input content, with the objective of retrieving this text upon clicking a button. Below is the corresponding HTML code snippet: <ion-row> <ion-item> <ion-textarea ...

How can we leverage Shared and Core modules alongside Feature modules in Angular development?

When developing my Angular application, I have adopted a specific architecture approach and included a shared.module.ts file in the shared folder. While leveraging lazy-loading in my app, I find myself puzzled about the necessary imports, declarations, and ...

Looking to incorporate Functional Components in React using the package "@types/react" version "^18.0.17"? Learn how here!

With the removal of the children prop from React.FC type, what is the new approach for typing components? ...