Reactjs Typescript:将函数作为类型传入接口

Reactjs Typescript:将函数作为类型传入接口,reactjs,typescript,redux,Reactjs,Typescript,Redux,我试图弄清楚如何从现有的Typescript函数中获取类型,并使用它来定义接口。我正在处理React项目,我想将动作创建者(函数)传递到道具界面,然后作为组件进入React组件 示例操作创建者: export function myFunction(foo: string = "bar") { return { type: "EXAMPLE_ACTION", payload: foo, } } 示例组件: import React, { Comp

我试图弄清楚如何从现有的Typescript函数中获取类型,并使用它来定义接口。我正在处理React项目,我想将
动作创建者
(函数)传递到
道具
界面,然后作为
组件
进入React组件

示例操作创建者:

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

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)

但这似乎过于冗长和多余,需要导入另一个导出。是否有其他方法解决此问题?

您可以在type位置使用
typeof
关键字来获取命名值的类型

在这种情况下,你会写

import { myFunction } from "actions";

export interface Props {
    myFunc: typeof myFunction;
}

当前收到错误的原因是TypeScript有两个不同的声明空间,一个用于值,一个用于类型<代码>函数定义了一个值,但不是一个类型。

啊,谢谢,效果很好。不知怎么的,我甚至都没想过
import { myFunction } from "actions";

export interface Props {
    myFunc: typeof myFunction;
}