Typescript: Pass function as type in interface


Samuel Hudec

I'm trying to figure out how to get a type from an existing Typescript function and use it to define an interface. I'm working on a React project and I want to pass ( action creatorfunctionality) into Propsan interface and then as a pass into a React Component Component<Props, State>.

Action creator example:

export function myFunction(foo: string = "bar") {
    return {
        type: "EXAMPLE_ACTION",
        payload: foo,
    }
}

Example component:

import React, { Component } from 'react'
import { connect } from "react-redux"
import { myFunction } from "actions"

export interface Props {
    // This is what I'm trying to and and it ends up in ts error
    myFunc: myFunction
}

class SomeComponent extends Component<Props, {}> {
    render() {
        return (
            <div>
                Example:
                <button onClick={this.props.myFunc("baz")}>Click to dispatch</button>
            </div>
        )
    }
}

export default connect(null, {
    myFunction
})(SomeComponent)

I thought this would work, but frankly it's a typescript bug:

[ts] Cannot find name 'myFunction'

I wonder if I need to define a separate variable to typepass it to the component like this:

export type myFuncType = (foo: string) => { type: string, payload: string }
export const myFunction: myFuncType = (foo: string) => {
    return {
        type: "EXAMPLE_ACTION",
        payload: foo,
    }
}

But this seems too verbose and redundant, requiring another export to be imported. Is there any other workaround?

Aluan Haddad

You can use keywords typeofin type position to get the type of a named value.

In this case you would write

import { myFunction } from "actions";

export interface Props {
    myFunc: typeof myFunction;
}

The reason you are currently getting the error is that TypeScript has two different declaration spaces, one for values ​​and one for types. functionDefine a value instead of a type.

Related


Typescript: Pass function as type in interface

Samuel Hudec I'm trying to figure out how to get a type from an existing Typescript function and use it to define an interface. I'm working on a React project and I want to pass ( action creatorfunctionality) into Propsan interface and then as a pass into a Re

Typescript: Pass function as type in interface

Samuel Hudec I'm trying to figure out how to get a type from an existing Typescript function and use it to define an interface. I'm working on a React project and I want to pass ( action creatorfunctionality) into Propsan interface and then as a pass into a Re

Typescript: Pass function as type in interface

Samuel Hudec I'm trying to figure out how to get a type from an existing Typescript function and use it to define an interface. I'm working on a React project and I want to pass ( action creatorfunctionality) into Propsan interface and then as a pass into a Re

Typescript: Pass function as type in interface

Samuel Hudec I'm trying to figure out how to get a type from an existing Typescript function and use it to define an interface. I'm working on a React project and I want to pass ( action creatorfunctionality) into Propsan interface and then as a pass into a Re

Typescript: Pass function as type in interface

Samuel Hudec I'm trying to figure out how to get a type from an existing Typescript function and use it to define an interface. I'm working on a React project and I want to pass ( action creatorfunctionality) into Propsan interface and then as a pass into a Re

Typescript pass type to generic interface

a lunatic I have this response type export interface IResponse { status: boolean; message: string; } Then I want to pass a custom interface to this type. const res: IResponse<{count: number}> = await ... How can I modify my type for res.countrecognition?

Typescript interface as function return type

Muid Here's what's new in Typescript. I have a question about Typescript's use of interfaces as function return types. I have this interface interface IPerson { name: string, age: number } If I assign it an object, it checks the type and rejects if t

TypeScript interface with function return type

Amurim Let's say I have 5 simplified functions, the first takes the object Apiused to make the call as a parameter and the second (optional) parameter as a parameter to the api call itself. // functions.ts function f1 (api: Api) { return () => { // returns a

Typescript interface as function return type

Muid Here's what's new in Typescript. I have a question about Typescript's use of interfaces as function return types. I have this interface interface IPerson { name: string, age: number } If I assign it an object, it checks the type and rejects if t

TypeScript interface function type with generics

Arrowwood I recently found the following code bytes in an open source repository: interface Use<I, C = context<I>> { <O>(fn: avvio.Plugin<O, I>, options?: O): C; } Simplifies to: interface F<A> { <B>(foo: A, bar: B): A; } How would I go about calling

Typescript interface as function return type

Muid Here's what's new in Typescript. I have a question about Typescript's use of interfaces as function return types. I have this interface interface IPerson { name: string, age: number } If I assign it an object, it checks the type and rejects if t

TypeScript interface with function return type

Amurim Let's say I have 5 simplified functions, the first takes the object Apiused to make the call as a parameter and the second (optional) parameter as a parameter to the api call itself. // functions.ts function f1 (api: Api) { return () => { // returns a

TypeScript interface function type with generics

Arrowwood I recently found the following code bytes in an open source repository: interface Use<I, C = context<I>> { <O>(fn: avvio.Plugin<O, I>, options?: O): C; } Simplifies to: interface F<A> { <B>(foo: A, bar: B): A; } How would I go about calling

TypeScript interface function type with generics

Arrowwood I recently found the following code bytes in an open source repository: interface Use<I, C = context<I>> { <O>(fn: avvio.Plugin<O, I>, options?: O): C; } Simplifies to: interface F<A> { <B>(foo: A, bar: B): A; } How would I go about calling

typescript - pass type to function parameter

LTFoReal I'm pretty new to Typescript, but I'm trying to pass a custom type to a function. In this case I avoid "any", here is my example. interface ReturnObj<T> { returnObj: T | null } const createResultObj = ( isSuccessful: boolean = false, return

typescript - pass type to function parameter

LTFoReal I'm pretty new to Typescript, but I'm trying to pass a custom type to a function. In this case I avoid "any", here is my example. interface ReturnObj<T> { returnObj: T | null } const createResultObj = ( isSuccessful: boolean = false, return

typescript - pass type to function parameter

LTFoReal I'm pretty new to Typescript, but I'm trying to pass a custom type to a function. In this case I avoid "any", here is my example. interface ReturnObj<T> { returnObj: T | null } const createResultObj = ( isSuccessful: boolean = false, return

typescript - pass type to function parameter

LTFoReal I'm pretty new to Typescript, but I'm trying to pass a custom type to a function. In this case I avoid "any", here is my example. interface ReturnObj<T> { returnObj: T | null } const createResultObj = ( isSuccessful: boolean = false, return

Pass generic type to function in typescript

frankenwood Consider the example: interface A { foo: string; } interface B { foo: string; } function a<T>() { function b(arg: T) { return arg.foo; ^^^^^^ Property 'foo' does not exist on type 'T'. } return b; } a<A>(); a<B>

Typescript - pass interface literal to function parameter

Paul Redmond I want to use an interface to set data types and then call them in a function while setting default values without passing the data . I am getting the after canvas error ',' expected.in the function . Can't I call it that? // Options interface opt

Typescript - pass interface literal to function parameter

Paul Redmond I want to use an interface to set data types and then call them in a function while setting default values without passing the data . I am getting the after canvas error ',' expected.in the function . Can't I call it that? // Options interface opt

Typescript - pass interface literal to function parameter

Paul Redmond I want to use an interface to set data types and then call them in a function while setting default values without passing the data . I am getting the after canvas error ',' expected.in the function . Can't I call it that? // Options interface opt

Typescript - pass interface literal to function parameter

Paul Redmond I want to use an interface to set data types and then call them in a function while setting default values without passing the data . I am getting the after canvas error ',' expected.in the function . Can't I call it that? // Options interface opt

Typescript - pass interface literal to function parameter

Paul Redmond I want to use an interface to set data types and then call them in a function while setting default values without passing the data . I am getting the after canvas error ',' expected.in the function . Can't I call it that? // Options interface opt

Pass array pointer to interface of function expected type

User 11924970: The example in the Golang book shows a pointer to an array passed by reference to a function: package main import "fmt" func reclassify(planets *[]string) { *planets = (*planets)[0:8] } func main() { planets := []string{ "mer

Pass array pointer to interface of function expected type

User 11924970: The example in the Golang book shows a pointer to an array passed by reference to a function: package main import "fmt" func reclassify(planets *[]string) { *planets = (*planets)[0:8] } func main() { planets := []string{ "mer

Pass array pointer to interface of function expected type

User 11924970: The example in the Golang book shows a pointer to an array passed by reference to a function: package main import "fmt" func reclassify(planets *[]string) { *planets = (*planets)[0:8] } func main() { planets := []string{ "mer

TypeScript interface function field contradicting type error

Harry Soloway I have the following type, which determines that all properties will be functions, takes no arguments, and cannot take arguments of a single type Record<string, any>: type FnTrait = Record< string, (input?: Record<string, any>) => any >; I t