带有参考的TypeScript项目

Dor*_*rad 7 project-structure typescript

我使用项目引用从“前面”和“后面”引用“共享”项目。

tsc -v: Version 3.3.3

项目结构:

./{MY_PROJECT}.code-workspace   /* the only file in this level */
./back
./back/tsconfig.json
./shared/src/
./shared/
./shared/tsconfig.json
./shared/src/
./front
./front/tsconfig.json
./front/src
Run Code Online (Sandbox Code Playgroud)

我正在尝试./front/src/article-view-model.ts从共享项目中导入模块:

import Article from "@shared/src/article";            // alias path
import Article from "../../shared/src/article"; // full relative path
export default class ArticleViewModel {
}
Run Code Online (Sandbox Code Playgroud)

VS Code GUI中会立即显示以下错误:

对于别名路径:

找不到模块'@ shared / src / article'。ts(2307)

对于完整的相对路径:

尚未从源文件“ c:/ {SOMEWHERE_IN_MY_PC} /shared/src/article.ts”构建输出文件“ ../../shared/src/article”。ts(6305)

Intellisense(VS Code)确实适用于别名和相对选项:

智能感知

如果我尝试忽略错误并构建,它将失败:

C:\ Program Files \ nodejs \ node_modules \ npm \ bin \ node_modules \ typescript \ lib \ tsc.js:1296 throw e; ^

错误:调试失败。错误的表达。在mergeSymbol(C:\ Program Files \ nodejs \ node_modules \ npm \ bin \ node_modules \ typescript \ lib \ tsc.js:25861:26)在C:\ Program Files \ nodejs \ node_modules \ npm \ bin \ node_modules \ typescript \在libs \ tsc.js:25960:47在Map.forEach()在mergeSymbolTable(C:\ Program Files \ nodejs \ node_modules \ npm \ bin \ node_modules \ typescript \ lib \ tsc.js:25958:20)在initializeTypeChecker(C :\ Program Files \ nodejs \ node_modules \ npm \ bin \ node_modules \ typescript \ lib \ tsc.js:48653:21)在Object.createTypeChecker(C:\ Program Files \ nodejs \ node_modules \ npm \ bin \ node_modules \ typescript \位于Object.getGlobalDiagnostics(C:\ Program Files \ nodejs \ node_modules \ npm \ bin \ node_modules \ typescript \ lib \ tsc.js:71398:93)处的getDiagnosticsProducingTypeChecker(lib:\ tsc.js:25711:9)

./front/tsconfig.json内容:

{
    "compilerOptions": {
        "allowSyntheticDefaultImports": true,
        "baseUrl": ".",
        "module": "amd",
        "noEmitOnError": true,
        "noImplicitAny": false,
        "out": "./lib/front-bundle.js",
        "paths": {"@shared/*" : ["../shared/*"]},
        "preserveConstEnums": true,
        "removeComments": true,
        "sourceMap": true,
        "target": "es2015",
        "watch": true
    },
    "include": [
        "./src/**/*.ts",
    ],
    "references": [
        {
            "path": "../shared"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

./shared/tsconfig.json内容:

{
    "compilerOptions": {
        "allowSyntheticDefaultImports": true,
        "composite": true,
        "declaration": true,
        "module": "amd",
        "noEmitOnError": true,
        "noImplicitAny": false,
        "out": "./lib/shared-bundle.js",
        "preserveConstEnums": true,
        "removeComments": true,
        "sourceMap": true,
        "target": "es2015",
        "watch": true
    },
    "include": [
        "./src/**/*.ts",
    ]
}
Run Code Online (Sandbox Code Playgroud)

Jaf*_*ake 8

我到达这里试图解决“尚未从源文件构建”错误。结果发现tsc监视进程没有正确退出,所以我有两个实例争夺文件。以防其他人遇到同样的问题


eps*_*lon 8

在搜索“尚未从源文件构建打字稿引用”时发现了这一点。我的错误是tsc -p tsconfig.json当我应该使用--build标志时我正在运行: tsc --build tsconfig.json。如果使用-pTypeScript运行将不会构建引用的项目。仅当您使用--build.


Dor*_*rad 5

我在这里的最后一次活动后几乎没有解决它,但我不能 100% 确定这是否是由于以下更改而发生的。无论如何,我在这里发布它,因为仍然有新的观点和投票,所以它可能对其他人有价值:

这就是变化:

./shared/tsconfig.json 内容中:而不是:

{
    "compilerOptions": {
        "allowSyntheticDefaultImports": true,
        "composite": true,
        "declaration": true,
        "module": "amd",
        "noEmitOnError": true,
        "noImplicitAny": false,
        "out": "./lib/shared-bundle.js", <-------------
        "preserveConstEnums": true,
        "removeComments": true,
        "sourceMap": true,
        "target": "es2015",
        "watch": true
    },
    "include": [
        "./src/**/*.ts",
    ]
}
Run Code Online (Sandbox Code Playgroud)

用:

{
    "compilerOptions": {
        "allowSyntheticDefaultImports": true,
        "composite": true,
        "declaration": true,
        "module": "amd",
        "noEmitOnError": true,
        "noImplicitAny": false,
        "outDir": "./lib/", <-------------
        "preserveConstEnums": true,
        "removeComments": true,
        "rootDir": "./src", <-------------
        "sourceMap": true,
        "target": "es2015",
        "watch": true
    },
    "include": [
        "./src/**/*.ts",
    ]
}
Run Code Online (Sandbox Code Playgroud)