config.js文件的打字稿版本

时间:2017-05-18 19:13:10

标签: node.js typescript

通常我会有一个如下所示的config.js文件:

config.js

module.exports = {
  secret: "sdlfjlsdjkflsdjfsdjflsdjf",
  terminal_id: '39493843',
  transaction_key: 'asdfsldkfjdslkfjsdl'
};

我会像这样使用它:

index.js

const config = require('./config');
console.log(config.secret);

这样做的打字稿方式是什么? config.ts文件是什么样的,如何在index.ts文件中导入它?

2 个答案:

答案 0 :(得分:2)

更接近原始设计的是:

<强> config.ts

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

<强> index.ts

export default {
  secret: "sdlfjlsdjkflsdjfsdjflsdjf",
  terminal_id: '39493843',
  transaction_key: 'asdfsldkfjdslkfjsdl'
};

您不需要声明配置属性的类型,typescript将从常量推断出。您仍然可以获得所有类型检查的优点和智能感知。

答案 1 :(得分:0)

根据Arpit Solanki的评论,我做到了这一点:

<强> config.ts

public function postLogin(Request $request) {
        $pushUser = \App\PushUser::where([
            'email' => $request->useremail,
            'password' => $request->userpassword
            ])->first();

        if(!$pushUser) {
            return Redirect::back(); // No such user in database, go back to login route
        }
        // Login user, make sure he stays logged in until he loggs out or the session expires
        return Redirect::route('home'); // Redirect to home route
    }

<强> index.ts

export const secret: string = 'sdlfjlsdjkflsdjfsdjflsdjf';
export const terminal_id: string = '39493843',
export const transaction_key: string = 'asdfsldkfjdslkfjsdl';