Avoid two time argument definition when passing function as argument typescript

rustyBucketBay

As far as I researched, if you want a function to be passed as an argument and used as a callback, you need to introduce in the method all the arguments that the callback function is going to use, and then once again for those same arguments, when you define the type of callback function in the arguments, so twice.

In my code example below, arguments between two asterisks have to be defined twice, because most of the arguments are for the callback function, of whom types have to be once again defined when you define the callback itself (see the last argument callbackFunc):

keyBoardListenerSet: (**gl:WebGLRenderingContext**, 
    document:Document, **primitiveType: GLenum**,
    **offset: number**, **count: number**, **program:WebGLProgram**,
    pos: Float32Array, rot: Float32Array, anglesChanged: {x:boolean, y:boolean, z:boolean},
    callbackFunc: (gl:WebGLRenderingContext, primitiveType: GLenum, offset: number,
    count: number, program: WebGLProgram) => void): void => {
    

    //really meaningful code


}

I was trying to pass the function with the arguments already set in a variable, and delay execution in my function to avoid argument type definition, but I think that is not possible (I'm new to Javascript), so if this is not possible, is there any less verbose way to use callback functions as arguments in typescript (by this I mean, a way to avoid set the type twice for the same argument that will be used in the callback function)? Thanks

Catalyst

You can refactor the type out into its own type declaration, and then you can also group the re-usable parameters into a single argument as an object:

e.g.


// could also be a Tuple if you don't want to type out param keys e.g. type A = [number, string]
type KeyboardListenerParam = {
    gl: WebGLRenderingContext,
    primitiveType: GLenum,
    offset: number,
    count: number,
    program: WebGLProgram,
    pos: Float32Array,
    rot: Float32Array,
    document: Document,
    anglesChanged: { x: boolean, y: boolean, z: boolean }
};

const something = {
    keyBoardListenerSet: (
        params: KeyboardListenerParam,
        callbackFunc: (params: KeyboardListenerParam) => void
    ): void => {
        window.requestAnimationFrame(() => {
            callbackFunc(params);
        });
        //really meaningful code
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to understand a function definition when passing it as an argument

TypeScript not inferring function argument type when passing inline function call

TypeScript: two argument types for function

TypeScript dynamic Function definition based on functions argument

Typescript complaining after passing ...args as argument to a function

Typescript: Passing in an inherited class name as an argument to a function

Passing javascript object as an argument to a function - typescript equivalent

Passing two values with parentheses in a single argument function

Typescript argument passing

Passing A Function As Argument To A Function

Segmentation fault when passing internal function as argument

Click event not triggering when passing function with argument

Receiving NaN when passing argument "0" to function

Error when passing function as an argument in C++

passing two argument in subquery

typescript spread and cast at the same time in the function argument

Function argument with two possible types in typescript

How to avoid evaluation of TFunc<TProc<T>> when passing as method argument?

Scala Passing Function with Argument

passing colormap as argument to function

passing argument to map function

Passing function as an argument: Python

passing a function as an argument in python

Passing Python function with no argument

On passing argument to a callback function

Passing a pointer as a function argument

Passing a function with an "implicit" argument

Passing in an array as argument to a function

Argument passing in decorated function

TOP Ranking

HotTag

Archive