小编Sam*_*dec的帖子

打字稿:在界面中将函数作为类型传递

我试图弄清楚如何从现有的Typescript函数中获取类型并使用它来定义接口.我正在研究React项目,我想将action creator(函数)传递给Props接口,然后传递给React Component as Component<Props, State>.

示例动作创建者:

export function myFunction(foo: string = "bar") {
    return {
        type: "EXAMPLE_ACTION",
        payload: foo,
    }
}
Run Code Online (Sandbox Code Playgroud)

示例组件:

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> …
Run Code Online (Sandbox Code Playgroud)

typescript reactjs redux

2
推荐指数
1
解决办法
3595
查看次数

标签 统计

reactjs ×1

redux ×1

typescript ×1