【问题标题】:TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'typeof ... renderIf"' has no compatible call signaturesTS2349:无法调用其类型缺少调用签名的表达式。类型 'typeof ... renderIf"' 没有兼容的调用签名
【发布时间】:2017-10-11 23:16:59
【问题描述】:

它基于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;
    }
}

【问题讨论】:

    标签: javascript typescript react-native reactxp


    【解决方案1】:

    哇....太疯狂了,实际上是因为我在调用renderIf时没有添加.default....但是我什至不能做import renderIf = require('./util/renderIf').default;所以我必须做import renderIf = require('./util/renderIf'); ..这很奇怪

    【讨论】:

      猜你喜欢
      • 2020-05-17
      • 2019-10-08
      • 1970-01-01
      • 1970-01-01
      • 2020-02-07
      • 2020-01-29
      • 1970-01-01
      • 1970-01-01
      • 2019-01-26
      相关资源
      最近更新 更多