如何将打字脚本代码分离为多个文件



以前我在C++开发方面经验丰富,在那里包含另一个文件非常容易。在打字稿中,它似乎并不那么琐碎。

有几个巨大的数组,我只想将它们声明为const一次,然后在稍后的核心逻辑中使用它们。由于这些数组的大小,我不想用它污染我的主代码,我想把它放在一个外部ts文件中。我试着在谷歌上搜索并包含每一种可能性,但似乎我做不好。

我的tsconfig.json:

{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"outDir": "out",
"lib": [
"es6"
],
"sourceMap": true,
"rootDir": "src",
"strict": true,   /* enable all strict type-checking options */
/* Additional Checks */
"noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
"noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
// "noUnusedParameters": true,  /* Report errors on unused parameters. */
},
"include": [
"src/*"
],
"exclude": [
"node_modules",
".vscode-test"
],
"files": [
"src/extension.ts",
"src/templates.ts",
"src/intellisense.ts",
]
}

主代码是扩展.ts,另外两个应该只包含一些常量变量。intelligense.ts:的示例代码

import * as vscode from 'vscode';
const completionTypes = [
new vscode.CompletionItem('text1'),
new vscode.CompletionItem('text2'),
new vscode.CompletionItem('text3'),
... // A hundred more entry
];

但是当我尝试在扩展中的代码中使用completionTypes时,linter说Cannot find name 'completionTypes'.ts(2304)

如何用打字脚本组织我的代码?

您需要导出completionTypes以使其可访问其他文件:

intelligense.ts:

import * as vscode from 'vscode';
export const completionTypes = [
// ...
];

分机.ts:

import { completionTypes } from './intellisense';
//...

最新更新