繁体   English   中英

VSCode 最低 TypeScript 项目

[英]VSCode Minimum TypeScript Project

编辑:我不想要或不需要浏览器,我只想在节点内运行所有内容。

我正在尝试在 VSCode 中创建一个 TS 项目并且遇到了困难。 我已经使用 VSCode 创建和运行 Angular 7-9 和 AngularJS 项目,但我不想通过所有设置/测试 go 来运行一些 TS 代码。 今晚之前我从未创建过“仅节点”项目,但我已经能够执行一个能够读取一个TS 文件但不能导入其他文件的 JS 文件。

我正在寻找 .tsconfig 设置、package.json 设置和“正常工作”的导入/要求语句的神奇组合。

index.js 现在的样子。 我尝试过 require、此导入和“ts 样式”导入,但错误程度不同(取决于接下来的 2 个文件)。 如果我不导入任何东西,console.log 执行得很好。 在某一时刻,我能够“导入”(或需要?)一个编译的 TS 文件,该文件没有导入任何其他 TS 文件,并且代码执行得很好。

    import ts from "./dist/start.js";
    console.log("starting");

package.json 目前的位置。 到目前为止,添加“type: module”和“--experimental-modules”感觉让我走得最远。

{
    "name": "test",
    "version": "1.0.0",
    "main": "index.js",
    "scripts": {
        "go": "tsc & node --experimental-modules index.js"
    },
    "dependencies": {},
    "devDependencies": {
        "typescript": "^3.9.3"
    },
    "type": "module"
}

tsconfig.json 当前所在的位置。 我尝试从 angular 项目中复制一个,摆弄 module/moduleResolution/target 设置,以及我在网上找到的其他一些东西。

{
    "compilerOptions": {
        "baseUrl": "./",
        "outDir": "./dist",
        "sourceMap": true,
        "declaration": false,
        "downlevelIteration": true,
        "experimentalDecorators": true,
        "module": "es2015",
        "moduleResolution": "node",
        "importHelpers": true,
        "target": "es2015",
        "lib": [
            "es2018",
            "dom"
        ]
    }
}

为了完整起见,我的 TS: cat.ts:

    export class Cat {
        constructor(public name: string) {}

        public meow(): void {
            console.log(this.name + " says 'Meow!'");
        }
    }

开始.ts:

    import { Cat } from "./cat"
    const cat = new Cat("sam");
    cat.meow();
    // just in case something needs to be exported.
    export const ts = "";

使用包裹捆绑器。 https://parceljs.org/

Parcel 将自动为您安装和配置大多数依赖项,包括 TypeScript。 这超级简单。

或者 go 到codesandbox.io并开始一个新项目,也许 Vanilla + TS 是您正在寻找的。 From the project editor you can select File -> Export to ZIP and download the project, including its package.json and tsconfig.json files. (Codesandbox.io 在引擎盖下使用包裹)

那里也有很多 TypeScript 样板代码库,但我喜欢 codeandbox.io 因为它超级快速且易于上手,而且您仍然可以下载项目文件。 而且,它基本上内置了 VSCode 编辑器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM