【问题标题】:typescript import doesn't work打字稿导入不起作用
【发布时间】:2016-11-21 14:57:07
【问题描述】:

我试图解决这个问题已经超过 6-8 个小时。 我认为问题之一是我的英语。 =)

我使用原子编辑器、电子、打字稿、反应

tsconfig.json

{
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "moduleResolution": "node",
        "isolatedModules": false,
        "jsx": "react",
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "declaration": false,
        "noImplicitAny": false,
        "noImplicitUseStrict": false,
        "removeComments": true,
        "noLib": false,
        "preserveConstEnums": true,
        "suppressImplicitAnyIndexErrors": true
    },
    "exclude": [
        "node_modules",
        "typings/browser",
        "typings/browser.d.ts"
    ],
    "compileOnSave": true,
    "buildOnSave": false,
    "atom": {
        "rewriteTsconfig": false
    }
}

app.ts:

import {hello} from "./func";
hello("TypeScript");

func.ts

export function hello(name: string){
    console.log(`Hello, ${name}`);
}

启动电子并得到错误:

Uncaught SyntaxError: Unexpected token import

然后,创建文件 react-test.ts:

import * as React from "react";

在原子编辑器中出错:

Cannot find module 'react'

问题: 我不想要更多的理论。有一个具体的例子。请帮我解决它。顺便说一句,我对 babel 没有任何问题 =)

更新 #1:

电子运行命令:

npm start

github上的项目

【问题讨论】:

  • 这可能是许多不同的问题,我们需要更多信息来了解问题所在。具体来说,你是如何运行电子的?你是如何运行 tsc 的?这和电子有什么关系?您的 index.html 文件是什么样的?你的package.json 文件是什么样的?
  • 我刚刚找到了一个示例导入文件并尝试实现它。电子运行命令npm start,github link。它只是电子快速入门示例 + typescript + react + redux

标签: javascript node.js reactjs typescript electron


【解决方案1】:

首先,您需要使用 tsc 命令将 TypeScript 转换为 JavaScript,并且在 index.html 文件中需要 app.js 而不是 app.ts。然后用npm start运行Electron

【讨论】:

  • 不错!最后,我理解了这个概念,并在这里找到了一个很好的例子link。斯帕西博米莎! =)
  • @user3650075 我很高兴它有帮助!
猜你喜欢
  • 2016-07-13
  • 2019-02-21
  • 2023-03-21
  • 1970-01-01
  • 2019-08-29
  • 1970-01-01
  • 2017-05-07
  • 2020-03-18
  • 1970-01-01
相关资源
最近更新 更多