如何为客户端传输typescript

如何为客户端传输typescript,typescript,Typescript,我试图在客户端使用我们的typescript代码,但当生成JS时,它会以以下内容开始每个文件: "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var foo_1 = require("./some file name"); 当试图在浏览器中执行时,我得到 未捕获引用错误:未定义导出 如果我删除了出口,它会抱怨需求 给出一个非常简单的例子,我可以重现这个 // create a file

我试图在客户端使用我们的typescript代码,但当生成JS时,它会以以下内容开始每个文件:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var foo_1 = require("./some file name");
当试图在浏览器中执行时,我得到

未捕获引用错误:未定义导出

如果我删除了出口,它会抱怨需求

给出一个非常简单的例子,我可以重现这个

// create a file foo.ts and put the following in it.
export class foo{
name:string = "Thomas";
}

// create another file called test.ts and put the following in it.
import { foo } from "./foo";
console.log(new foo().name);

// tsconfig.json
    {
        "compilerOptions": {
            "target": "es5", //defines what sort of code ts generates, es5 because it's what most browsers currently UNDERSTANDS.        
            "moduleResolution": "node",
            "sourceMap": true,
            "removeComments": true,
            "noImplicitAny": false,
            "lib": [
                "es2016",
                "dom",
                "es5"
            ]
        }
    }

我使用的是Typescript 3.0.1,默认情况下,Typescript编译器生成CommonJS模块,浏览器无法直接运行。您需要使用诸如Webpack、Browserify、Rollup或RequireJS和set-TypeScript的模块编译器选项来生成您选择的加载程序或绑定程序所接受的模块格式。或者,如果您将TypeScript设置为生成es6模块,则可以在最近的浏览器中使用本机模块支持;参见,例如