Previously I'm mostly experienced in C++ development, where including another file is pretty easy. In typescript it doesn't seem to be so much trivial.
There are a few huge arrays, which I'd like to declare only once as const and then use them later in my core logic. Because of the size of those arrays I wouldn't like to pollute my main code with it, I'd like to put it into an external ts file. I've tried to google and include every single possibility, but it seems I can't get it right.
My 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",
]
}
Main code is the extension.ts, the other two should only contain some constant variables. Sample code from intellisense.ts:
import * as vscode from 'vscode';
const completionTypes = [
new vscode.CompletionItem('text1'),
new vscode.CompletionItem('text2'),
new vscode.CompletionItem('text3'),
... // A hundred more entry
];
But when I try to use that completionTypes
in my code in extension.ts the linter says Cannot find name 'completionTypes'.ts(2304)
How is it possible to organize my code in typescript?
You need to export the completionTypes
to make it accessible to other files:
intellisense.ts:
import * as vscode from 'vscode';
export const completionTypes = [
// ...
];
extension.ts:
import { completionTypes } from './intellisense';
//...
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments