TS2349:无法调用类型缺少调用签名的表达式。输入' typeof ... renderIf"'没有兼容的呼叫签名

时间:2017-05-12 15:10:49

标签: javascript typescript react-native reactxp

基于https://github.com/react-native-training/reactxp-starter 我也是TypeScript的新手!

renderIf是一个常规的js函数,由于某种原因,我得到了类型的错误。我不清楚应该让renderIf函数运行的类型。任何想法或指针都会很棒!因为ReactXP实际上是React Native之上的一个瘦包装器。

谢谢!

/*
* This file demonstrates a basic ReactXP app.
*/

import RX = require('reactxp');
import renderIf = require('./util/renderIf');
//import { VirtualListView, VirtualListViewItemInfo } from 'virtuallistview';

const styles = {
    container: RX.Styles.createViewStyle({
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#0a84c1'
    }),
    helloWorld: RX.Styles.createTextStyle({
        color: 'white',
        fontSize: 48,
        fontWeight: 'bold',
        marginBottom: 28
    }),
    welcome: RX.Styles.createTextStyle({
        color: 'white',
        fontSize: 32,
        marginBottom: 12
    }),
    instructions: RX.Styles.createTextStyle({
        fontSize: 16,
        color: '#aaa',
        marginBottom: 40
    }),
    docLink: RX.Styles.createLinkStyle({
        fontSize: 16,
        color: 'blue'
    }),
    emotionalInput: RX.Styles.createLinkStyle({
        fontSize: 32,
        height: 32,
    }),

    questionPage: RX.Styles.createTextStyle({
        color: 'white',
        fontSize: 48,
        fontWeight: 'bold',
        marginBottom: 28
    }),

    questionPageShow: RX.Styles.createTextStyle({
        color: 'white',
        fontSize: 48,
        fontWeight: 'bold',
        marginBottom: 28
    }),

    questionPageHide: RX.Styles.createTextStyle({
        color: 'white',
        fontSize: 48,
        fontWeight: 'bold',
        marginBottom: 28,
    }),
};

// interface QuestionItem extends VirtualListViewItemInfo {
//     text: string;
// }
interface QuestionProps {
}
interface QuestionState {
    questionNow: number;
}

class App extends RX.Component<QuestionProps, QuestionState> {
    private _translationValue: RX.Animated.Value;
    private _translationValue2: RX.Animated.Value;
    private _animatedStyle: RX.Types.AnimatedTextStyleRuleSet;
    private _animatedStyleNextQuestion: RX.Types.AnimatedTextStyleRuleSet;

    constructor() {
        super();

        this.state = {
            questionNow: 1,
        };

        this._translationValue = new RX.Animated.Value(-100);
        this._animatedStyle = RX.Styles.createAnimatedTextStyle({
            transform: [
                {
                    translateY: this._translationValue
                }
            ]
        });

        this._translationValue2 = new RX.Animated.Value(-100);
        this._animatedStyleNextQuestion = RX.Styles.createAnimatedTextStyle({
            transform: [
                {
                    translateY: this._translationValue2
                }
            ]
        });
    }

    componentDidMount() {
        // let animation = RX.Animated.timing(this._translationValue, {
        //       toValue: 0,
        //       easing: RX.Animated.Easing.OutBack(),
        //       duration: 500
        //     }
        // );
        // animation.start();
        let animationParalle = RX.Animated.parallel([
            RX.Animated.timing(this._translationValue, {
              toValue: 0,
              easing: RX.Animated.Easing.OutBack(),
              duration: 1000
            }),
            RX.Animated.timing(this._translationValue2, {
              toValue: 0,
              easing: RX.Animated.Easing.OutBack(),
              duration: 3000
            }),
        ]);

        animationParalle.start();
    }
    guessResult(e: any) {
        console.log(e);
    }
    pressAction(e: any) {
        console.log(e);
    }
    goNext(event: any) {
        console.log(event);
        const newCount = this.state.questionNow + 1;
        this.setState({
            questionNow: newCount,
        });
        console.log(newCount);
    }

    render(): JSX.Element | null {
        console.log(renderIf);
        return (
            <RX.View style={ styles.container }>
                <RX.Animated.Text style={ [styles.helloWorld, this._animatedStyle] }>
                    Go Go Go
                </RX.Animated.Text>

                <RX.Animated.Text style={ [styles.questionPage, this._animatedStyleNextQuestion] }>
                    NEXT Question!
                </RX.Animated.Text>

                {renderIf(
                    this.state.questionNow === 1,
                    <RX.Text> New Stuff</RX.Text>
                )}

                {/* 
                    Question 1
                */}
                <RX.Text style={ styles.welcome }>
                    Question {this.state.questionNow}
                    How are you?
                    <RX.Button
                        style={ styles.welcome }
                        onPressOut={this.goNext.bind(this)}>
                        Great!
                    </RX.Button>

                    <RX.Button
                        style={ styles.welcome }
                        onPressOut={this.goNext.bind(this)}>
                        Great!
                    </RX.Button>
                </RX.Text>



                {/* 
                    Question 2
                */}
                <RX.Text style={ styles.welcome }>
                    Question {this.state.questionNow}
                    How are you?
                    <RX.Button
                        style={ styles.welcome }
                        onPressOut={this.goNext.bind(this)}>
                        Great!
                    </RX.Button>

                    <RX.Button
                        style={ styles.welcome }
                        onPressOut={this.goNext.bind(this)}>
                        Great!
                    </RX.Button>
                </RX.Text>




                <RX.TextInput style={ styles.emotionalInput } onChangeText={this.guessResult}/>
                <RX.Text>Text</RX.Text>
            </RX.View>
        );
    }
}

export = App;

错误

ERROR in [at-loader] ./src/App.tsx:154:18
    TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'typeof "/Users/app/src/util/renderIf"' has no compatible call signatures.

renderIf.ts

export default function renderIf(condition: any, content: any) {
    if (condition) {
        return content;
    } else {
        return null;
    }
}

1 个答案:

答案 0 :(得分:1)

哇....它很疯狂,实际上是因为当我打电话给import renderIf = require('./util/renderIf').default;时我没有添加import renderIf = require('./util/renderIf'); ....但我真的不能target所以我必须做selector ..这很奇怪