简体   繁体   English

将 typescript 转换为 java 脚本时的奇怪行为

[英]Strange behavior when converting typescript to java script

I am trying to move my NodeJs app from Java Script to TypeScript to take advantage of some of the avail benefits.我正在尝试将我的 NodeJs 应用程序从 Java 脚本移动到 TypeScript 以利用一些可用的好处。 But while doing so i fail at the most simple taks..但是在这样做的同时,我在最简单的任务中失败了..

I have this simple code below in my index.ts file.我的 index.ts 文件中有下面这个简单的代码。 I want to import express and then start a basic listener on the port from the config file.我想导入 express,然后从配置文件在端口上启动一个基本侦听器。

import {express} from 'express';
import {config} from './config/config';
    
const app = express();
const http = require('http');

const secure = http.createServer(app).listen(config.port ,()=>{
    console.log(`listening on Port ${config.port}`)
})

when i run "npm run-script build" i get the below output当我运行“npm run-script build”时,我得到以下 output

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = require("express");
const config_1 = require("./config/config");
const app = express_1.express();
const http = require('http');
var secure = http.createServer(app).listen(config_1.config.port, () => {
    console.log(`listening on Port ${config_1.config.port}`);
});

why does it prefix express and config with _1?为什么它在 express 和 config 前加上 _1? The express_1.express seems to brake it. express_1.express 似乎制动它。 Hope someone can tell my why and how to avoid in future.希望有人能告诉我为什么以及将来如何避免。

I think express module is not a named export it's a default export.我认为 express 模块不是命名导出,它是默认导出。 for that you've to import like below to make it work.为此,您必须像下面这样导入才能使其工作。

import express from 'express';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM