gpt4 book ai didi

javascript - 如何从 index.js 获取 TS 文件?

转载 作者:行者123 更新时间:2023-11-30 13:50:39 26 4
gpt4 key购买 nike

我刚刚从 react-express-starter 创建了一个应用程序。在里面我有一个“服务器”文件夹,nodejs 使用它来创建后端。在此文件夹中,我有一个 index.js 文件:

const express = require('express');
const app = express();

const { Test } = require('./test/index.ts');

app.listen(3001, () => {
console.log('Express server is running on localhost:3001');
Test.run();
});

我需要从一个ts文件中调用一个方法,

服务器/测试/test.ts:

export class Test {
static run(){
console.log('OK THIS WORKS');
}
}

服务器/测试/index.ts

export { Test as test} from './test';

运行服务器:

export { Test as test } from './test';
^^^^^^

SyntaxError: Unexpected token export

另一种尝试,将此添加到 index.js 的第一行:

import { Test as test } from './test';

结果:

import { Test as test } from './test';
^

SyntaxError: Unexpected token {

现在更改 index.js 中的第一行:

import * as test from './test';

结果:

import * as test from './test';
^

SyntaxError: Unexpected token *

我如何让事情正常进行?

最佳答案

基本上我们必须将 typescript 文件从 ts 编译为 js。然后只有我们可以导入到js文件中。因此,我建议所有后端代码都应使用 javascript 或 typescript 进行维护

您可以使用上述任何其他入门项目

关于javascript - 如何从 index.js 获取 TS 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58324712/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com