gpt4 book ai didi

javascript - 意外的 token 导入/导出 - typescript

转载 作者:搜寻专家 更新时间:2023-10-30 20:42:53 24 4
gpt4 key购买 nike

我是第一次尝试 typescript ,对我习惯使用 es6 的导入/导出过程感到困惑。

这是我试图导出到名为 transformedRowInterface.ts 的文件中的接口(interface):

export interface TransformedRow  {
id: number;
title: string;
summary: string;
body: string;
synopsis: string;
author: object;
impressions: number;
created: number;
updated: number;
}

这是我尝试在名为 newsArticleModel.ts 的文件中导入:

const appRoot = require("app-root-path");

import { TransformedRow } from "./transformedRowInterface";
//throws the error below:
// [Node] /newsArticleModel.ts:2
// [Node] import { TransformedRow } from "./transformedRowInterface";
//SyntaxError: Unexpected token import
// also tried a require below, which also throws an error:
// const transformedRow = require(appRoot + "/src/controllers/transformedRowInterface.ts");
// throws this error:
// [Node] (function (exports, require, module, __filename, __dirname) { export interface TransformedRow {
// [Node] ^^^^^^
// [Node]
// [Node] SyntaxError: Unexpected token export

这是我的 tsconfig:

    {
"compilerOptions": {
"module": "commonjs",
"target": "es2017",
"noImplicitAny": false,
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist",
"baseUrl": ".",
"paths": {
// "*": ["node_modules/*", "src/types/*"]
}
},
"include": ["src/**/*"]
}

我做错了什么?

最佳答案

我很确定这是因为您的目标是 ES2017,它支持“开箱即用”的导入语法,即您的输出字面意思是包含

import { thing } from './wotsit';

如果您的运行时不支持这种导入,您将需要使用下层编译(即目标 ES5),以便将导入转换为 CommonJS require 调用。

您可以通过查看 JavaScript 输出以查看导入的样子来检验我的理论。

关于javascript - 意外的 token 导入/导出 - typescript ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46611503/

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