The specified type 'BallInterface' must have 2 type arguments specified

Currently on my Typescript learning journey, I encountered an error that states

Generic type 'BallInterface' requires 2 type argument(s)
in relation to tennisBall. How can I properly call a function with an object parameter containing multiple generic types? What is causing this error to be thrown? Below is the complete code snippet:

const addId = <T extends object>(obj: T) =>{ 
    const id = 3;
    return {
        ...obj,
        id
    }
}

interface BallInterface<T, V>{
    name: string
    data: T
    meta: V
}

const ball: BallInterface<{meta: string}, string> = {
    name: "Tennis",
    data:{meta: "for playing"},
    meta: "Mario"
}

const tennisBall = addId<BallInterface>(ball);

Answer №1

The reason for the error is due to your definition of BallInterface as an interface with two generics.

When you call addId on the last line, you are passing BallInterface as a generic without specifying the values that T and V should have.

It's not entirely clear what you are trying to accomplish here. If all you need to do is add the field Id, there is no need to provide a generic to addId at all. You are overcomplicating things unnecessarily :)

const addId = (obj: object) =>{
    const id = 3;
    return {
        ...obj,
        id
    }
}

interface BallInterface<T, V>{
    name: string
    data: T
    meta: V
}

const ball: BallInterface<{meta: string}, string> = {
    name: "Soccer",
    data:{meta: "for playing"},
    meta: "Ronaldo"
}

const soccerBall = addId(ball);

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

An exception is thrown by TypeScript's readline.createInterface function

My current project involves creating an electron app. I am facing an issue in the main.ts file where I have constructed a seemingly simple class with a constructor that is not running as expected. The problem arises when the call to readline.createInterfac ...

Determine the type of input and output based on another argument

When working with a function that takes an object of either TypeA or TypeB, the first parameter is used to specify the type of the object and the returned type depends on this first parameter. The issue arises in TypeScript where the type of the object is ...

Utilizing declaration files in a TypeScript application to provide global exposure of types

Recently, I've delved into the world of typescript and set up a project with numerous React components written in typescript. To streamline development, we decided to extract all necessary types for these components into a separate file named types.d. ...

Can someone explain the distinction between 'return item' and 'return true' when it comes to JavaScript array methods?

Forgive me for any errors in my query, as I am not very experienced in asking questions. I have encountered the following two scenarios :- const comment = comments.find(function (comment) { if (comment.id === 823423) { return t ...

What does this.userSubject = new BehaviorSubject<User>(JSON.parse(localStorage.getItem('user'))); signify?

I'm attempting to deconstruct this specific code snippet to comprehend it better. The new BehaviourSubject appears to be a function call, but I'm confused about what it's doing there. Is it instructing the function BehaviourSubject to have a ...

One function in Typescript lodash is missing a default export

Is there a way to import just one function from lodash? I attempted it like this: import get from 'lodash/get'; Even after installing both lodash and @types/lodash, I encountered the following error message: @types/lodash/get/index"' ha ...

What is the best way to test a callback function of a React component that is encapsulated with withRouter()?

In my Jest and Enzyme testing of a TypeScript-written React project, I have a component wrapped in a React-Router router. Here's a snippet of the code: import { SomeButton } from './someButton'; import { RouteComponentProps, withRouter } fr ...

Custom Angular Form Control Implementation

Greetings! I'm currently in the process of developing a collection of custom reactive form controls to make it easier for templates. So far, I've successfully created one using the ControlValueAccessor interface. The form editing functionality is ...

ReactJS: What happens when props aren't in array form?

Newcomer question: Just starting to dive into ReactJS+Typescript. I'm working with a simple interface that describes an array of data: interface IProfile { name: string; avatar_url: string; company: string; } const testData: IProfile[] = [ { ...

Adding dynamic URLs to the canonical tags in Next.js version 14.2.4: A comprehensive guide

I'm currently facing an issue with implementing dynamic URLs in my Next.js dynamic routes. To generate metadata, I am utilizing a server-rendered page: export const metadata: Metadata = { title: `InterviewQA | ItsIndianGuy`, description: ...

Can you explain the distinction between certain assignment assertion and ambient declaration?

When declaring that a field is definitely initialized within a class, what distinguishes the ! (exclamation point, definite assignment assertion) from the declare modifier? The subsequent code triggers an error in strict mode as TypeScript cannot confirm ...

Unable to find solutions for all parameters needed by a CustomComponent within Angular

Whenever I attempt to compile the project for production, an error pops up: There is a problem resolving all parameters for EmployeeComponent in C:/.../src/app/employee/employee.component.ts: (?, ?, ?). Oddly enough, when I run the application, every ...

Creating a React FunctionalComponent in Typescript without explicitly assigning it as a function

In my recent exploration of code, I stumbled upon a segment where FormWithRedirect is defined as a FC(FunctionComponent): declare const FormWithRedirect: FC<FormWithRedirectProps>; export declare type FormWithRedirectProps = FormWithRedirectOwnProps ...

Utilize React's useState hook in combination with TypeScript to effectively set a typed nested object

In my project, I have a burger menu component that will receive two props: 1) isOpen, and 2) a file object { name, type, size, modifiedAt, downloadUrl } I'm attempting to implement the following code snippet, but I am encountering issues with Typescr ...

When a const variable is declared within the composition-api setup(), it remains unchanged unless redeclared within the function scope

Being primarily a back-end developer, the front-end side of things is still relatively new to me. I'm aware that there are concepts in this domain that I haven't fully grasped yet, and despite my efforts, I'm unable to resolve a particular i ...

You can't assign the type "any [] | undefined" to the type "any []"

Ever since I updated my npm and all modules today, I've run into a new problem. Everything was working perfectly fine before the update. The error message reads: "the type 'any [] | undefined' cannot be assigned to the type 'any []&apo ...

Solve the issue of the __typename union

Imagine having the following union: export type IBookmarkItemFragment = | ({ __typename: "Story" } & { story: number; }) | ({ __typename: "Product" } & { product: number; }) | ({ __typename: "Project" } & { proj ...

"Encountered a runtime error while trying to execute the doubleClick() function using Pro

Encountering the following issue: "WebDriverError: Unable to convert: Error 404: Not found" while running a test with protractor: browser.actions().doubleClick(elem).perform(); or browser.actions().click(elem).click(elem).perform(); Uncertain of t ...

What is the reason for the input type number in the <input> field being passed as a string?

I am currently developing a straightforward component that allows the user to input the balance they wish to add. Here is the template used for taking input from the user: <h3>Add Balance:</h3> <input #payment type="number"> < ...

Having difficulty invoking the forEach function on an Array in TypeScript

I'm currently working with a class that contains an array of objects. export interface Filter { sf?: Array<{ key: string, value: string }>; } In my attempt to loop through and print out the value of each object inside the array using the forE ...