相关疑难解决方法(0)

TypeScript中是否可以使用强类型函数作为参数?

在TypeScript中,我可以将函数的参数声明为Function类型.是否存在一种"类型安全"的方式来解决这个问题?例如,考虑一下:

class Foo {
    save(callback: Function) : void {
        //Do the save
        var result : number = 42; //We get a number from the save operation
        //Can I at compile-time ensure the callback accepts a single parameter of type number somehow?
        callback(result);
    }
}

var foo = new Foo();
var callback = (result: string) : void => {
    alert(result);
}
foo.save(callback);
Run Code Online (Sandbox Code Playgroud)

保存回调不是类型安全的,我给它一个回调函数,其中函数的参数是一个字符串,但我传递一个数字,并编译没有错误.我可以在保存类型安全功能时创建结果参数吗?

TL; DR版本:在TypeScript中是否有等效的.NET委托?

typescript

491
推荐指数
7
解决办法
24万
查看次数

如何在打字稿中声明函数类型

我正在使用打字稿并将一个函数传递给另一个函数。

如果我有一个函数传递到打字稿中的另一个函数中,我应该如何编写类型?

我已经尝试过successHandler: function,但这似乎不起作用。

export function useSubscription(address: string, successHandler: function) {
   successHandler(address)
}
Run Code Online (Sandbox Code Playgroud)

typescript

15
推荐指数
2
解决办法
3万
查看次数

使用 TypeScript 响应 prop 类型 - 如何拥有函数类型?

是否有使用 TypeScript 的 React prop 类型的函数类型的最佳实践?

我认为这会起作用,但实际上它出错了:

type Props = {
  onClick: Function
};

const Submit = ({ onClick }: Props) => {
  return (
    <button type="button" onClick={onClick}>
      click me
    </button>
  );
Run Code Online (Sandbox Code Playgroud)

按照这个线程我让它工作了,但它似乎不必要地冗长:https : //github.com/Microsoft/TypeScript/issues/20007

type Props = {
  onClick: (...args: any[]) => any;
};

const Submit = ({ onClick }: Props) => {
  return (
    <button type="button" onClick={onClick}>
      click me
    </button>
  );
Run Code Online (Sandbox Code Playgroud)

typescript reactjs

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

如何使用TypeScript为React组件回调指定函数参数?

我在TypeScript中有一个React组件,它看起来像这样:

interface FooDetails {
    name: string
    latitude: number,
    longitude: number,
}

interface FooSelectorProps {
    onDetailsChanged: Function,
}

class FooSelector extends React.Component<FooSelectorProps, any> {
    constructor(props: FooSelectorProps, context) {
        super(props, context);
    }

    onTravelTypeChange(event) {
        this.setState({ travelType: event.target.value });
    }

    onDetailsChange(fooData) {
        this.props.onDetailsChanged({
            name: fooData.name,
            latitude: fooData.latitude,
            longitude: fooData.longitude,
        });
    }

    render() {
            return <div>...Foo selection UI...</div>
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望能够指定onDetailsChanged我的propTypes(FooSelectorProps)中的函数接受一个 FooDetails对象,但我无法弄清楚如何做到这一点.

typescript reactjs

10
推荐指数
1
解决办法
1万
查看次数

打字稿错误 - 不要将函数用作类型

TypeScript我已经开始在我的React-Native项目中使用

我的 linter 不喜欢函数作为类型,我如何重构我的 hoc 来传递我的 linter?

应用程序导航器:

const withHeader = (
    screen: Function,
    routeName: string,
    Header,
): StackNavigator =>
    createStackNavigator(
        {
            [routeName]: {
                screen,
                navigationOptions: ({routeName, props}) => ({
                    header: props => <Header {...props} />,
                }),
            },
        },
        {
            initialRoute: 'Home',
            cardStyle: {
                backgroundColor: 'transparent',
                opacity: 1,
            },
            transitionConfig: () => ({
                containerStyle: {
                    backgroundColor: 'transparent',
                },
            }),
        },
    );
Run Code Online (Sandbox Code Playgroud)

我的打包程序还给我提供了同一文件的错误:

error: bundling failed: Error: Unable to resolve module `./navigation/AppNavigator` from `C:\Users\Matt\sites\IAapp\App.tsx`: The module `./navigation/AppNavigator` could …
Run Code Online (Sandbox Code Playgroud)

typescript react-native

5
推荐指数
0
解决办法
1万
查看次数

标签 统计

typescript ×5

reactjs ×2

react-native ×1