Javascript 意外的令牌导入/导出-typescript
我是第一次尝试typescript,对es6中使用的导入/导出过程感到困惑 这是我试图在名为Javascript 意外的令牌导入/导出-typescript,javascript,typescript,Javascript,Typescript,我是第一次尝试typescript,对es6中使用的导入/导出过程感到困惑 这是我试图在名为transformedRowInterface.ts的文件中导出的接口: export interface TransformedRow { id: number; title: string; summary: string; body: string; synopsis: string; author: object; impressions: number; cre
transformedRowInterface.ts
的文件中导出的接口:
export interface TransformedRow {
id: number;
title: string;
summary: string;
body: string;
synopsis: string;
author: object;
impressions: number;
created: number;
updated: number;
}
这是我尝试导入的,在一个名为newsactionmodel.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),以便将导入转换为commomjs require调用
您可以通过查看JavaScript输出来测试我的理论,看看导入是什么样子的。(函数(导出、要求、模块、文件名、目录名){export})
SyntaxError:意外的令牌导出。
如果你遇到同样的问题。可能你正在做的是node.ts,应该是node.js,谢谢!我将其改为ES2016,现在一切正常。